KEMBAR78
Python-List comprehension | PDF
{ x|x > 1, x ∈ N }
 List Comprehension - Quick Generation of List




NCCU, Department of Computer Science
Python Programming for Non-Programmer
List Comprehension
                                Math World:
   •   Components
                                   S = {2 · x|x ∈ N, x < 4}
       •   for

       •   if
                               Python World:
       •   else
                               S = [2*x for x in range(1,20) if x<4]



NCCU, Department of Computer Science
Python Programming for Non-Programmer             {x}  List Comprehension
                                                       Introduction
Case 1

   • Q: Generate a list contains square of
       numbers between 1 and 20.
   • A:
       [x**2 for x in range(1,20+1)]



NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}  List Comprehension
                                             Introduction
Case II

   • Q: Give dictionary D as following:
       {ā€˜a’:1, ā€˜b’:2, ā€˜c’:3, ā€˜d’:4, ā€˜e’:5}
       Please generate the letters who has the
       value which is greater than or equal 3.
   • A: for x in D.keys() if D[x]>=3]
     [x
       [k for k,v in D.items() if v>=3]


NCCU, Department of Computer Science
Python Programming for Non-Programmer     {x}
                                            List Comprehension
                                            Introduction
Practice


   • Review assignment IV
   • Warm up for assignment V


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}
                                          List Comprehension
                                          Introduction
Practice

   • Q:
       It’s about height of people, please design a
       program to summarize and statistic the
       data in data file given in TA session.
   • Data file: pastie.org/1393033
   • Architecture file: pastie.org/1393073
NCCU, Department of Computer Science
Python Programming for Non-Programmer     {x}  List Comprehension
                                               Introduction
Functions

   • Load from file
   • Summary of heights
   • Lookup a person’s height
   • Person above a height
   • Print height report (three in a row)
NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x} List Comprehension
                                            Introduction
Load from file

   • Load data from a file into a dictionary
   • Key: name
   • Value: height
   • Hint: as what last assignment does

NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}   List Comprehension
                                              Introduction
Summary the Height

   • Print out the following three information:
       - Highest height
       - Lowest height
       - Average height
   • Hint: try to sort the data by values, and pick
       up the head and the tail of the sequence.


NCCU, Department of Computer Science
Python Programming for Non-Programmer    {x} List Comprehension
                                             Introduction
Lookup a Height


   • Ask a name and query the height of it
   • Hint: as what last assignment does


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}  List Comprehension
                                             Introduction
Person Above Height


   • Find out who is taller than specific height
   • Hint: use ā€œList Comprehensionā€


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x} List Comprehension
                                            Introduction
Print Report
   • Enumerate all people and their height each
       by each
   • Make output three records in row, like
       following:
       a, 152; b,180; c, 190;
       d, 161; e, 229; f, 191;
       g, 175; h, 159;
   • Hint: Embedded a counter to your loop
NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}
                                          List Comprehension
                                          Introduction
That’s all, just do it.


NCCU, Department of Computer Science
Python Programming for Non-Programmer   {x}
                                          List Comprehension
                                          Introduction

Python-List comprehension

  • 1.
    { x|x >1, x ∈ N } List Comprehension - Quick Generation of List NCCU, Department of Computer Science Python Programming for Non-Programmer
  • 2.
    List Comprehension Math World: • Components S = {2 Ā· x|x ∈ N, x < 4} • for • if Python World: • else S = [2*x for x in range(1,20) if x<4] NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 3.
    Case 1 • Q: Generate a list contains square of numbers between 1 and 20. • A: [x**2 for x in range(1,20+1)] NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 4.
    Case II • Q: Give dictionary D as following: {ā€˜a’:1, ā€˜b’:2, ā€˜c’:3, ā€˜d’:4, ā€˜e’:5} Please generate the letters who has the value which is greater than or equal 3. • A: for x in D.keys() if D[x]>=3] [x [k for k,v in D.items() if v>=3] NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 5.
    Practice • Review assignment IV • Warm up for assignment V NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 6.
    Practice • Q: It’s about height of people, please design a program to summarize and statistic the data in data file given in TA session. • Data file: pastie.org/1393033 • Architecture file: pastie.org/1393073 NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 7.
    Functions • Load from file • Summary of heights • Lookup a person’s height • Person above a height • Print height report (three in a row) NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 8.
    Load from file • Load data from a file into a dictionary • Key: name • Value: height • Hint: as what last assignment does NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 9.
    Summary the Height • Print out the following three information: - Highest height - Lowest height - Average height • Hint: try to sort the data by values, and pick up the head and the tail of the sequence. NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 10.
    Lookup a Height • Ask a name and query the height of it • Hint: as what last assignment does NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 11.
    Person Above Height • Find out who is taller than specific height • Hint: use ā€œList Comprehensionā€ NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 12.
    Print Report • Enumerate all people and their height each by each • Make output three records in row, like following: a, 152; b,180; c, 190; d, 161; e, 229; f, 191; g, 175; h, 159; • Hint: Embedded a counter to your loop NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction
  • 13.
    That’s all, justdo it. NCCU, Department of Computer Science Python Programming for Non-Programmer {x} List Comprehension Introduction