First, List parsing
List parsing comes from a functional programming language (Haskell) with the following syntax:
Inif cond_expr]
The first syntax: Iterate all the contents of iterable, each iteration, put the corresponding content in the iterable into Iter_var, then apply the Iter_var content in the expression, and then generate a list with the calculated value of the expression.
The second syntax: added the judgment statement, only satisfies the condition the content to put in the iterable the corresponding content in the Iter_var, then applies in the expression the Iter_var the content, finally uses the expression computation value to generate a list.
Examples are as follows:
>>> seq = [11,10,9,9,5,35,8,20,31,72,54,53]>>> filter (lambda x:x%2, seq) [11, 9, 9, 5, 35, x%2][11, 9, 9, 5, +, (+)
More complex, such as The matrix:
#3 rows 5 columns of matrix >>> [(x+1,y+1)For XIn range (3)For YIn range (5) [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4),)]# simple 3 rows 3 columns >>> [(x, Y) for x in range (3) for y in range (3# simulation >> > for x in range (3for y in range (3print X, y 0 10 21 22 1
Calculate the word count and size of a file:
>>>Importos>>> f = open (‘Demo.txt‘,‘R‘) >>>#Calculate the number of words >>> len ([WordFor line.split ()] 1942>>> # calculate file size >>> os.stat (' C8>demo.txt') .st_size14051l>>> f.seek (0) #Seek () function goes back to the file header because the iterator has accessed all the lines of the file >> > sum ([Len (word) for line in F for word in line.split ()]) 10806>>> f.close () >>>
Classic examples:
books=[ {"Name": U"C # from getting started to mastering","Price": 23.7,"Store": U"Excellence"}, {"Name": U"Asp. NET Advanced Programming","Price": 44.5,"Store": U"Excellence"}, {"Name": U"Python core programming","Price": 24.7,"Store": U"When you"}, {"Name": U"JavaScript Encyclopedia","Price": 45.7,"Store": U"When you"}, {"Name": U"Django Concise Tutorial","price" store" Xinhua bookstore "}, {" name deep Python" "price ": 55.7,store" Xinhua bookstore " }, "
1, the lowest price in the book information
#No1: Original Way:>>> price=[]>>>For itemin books: for p in item: if p = = "price : Price.append (Item[p]) >>> min (price) 23.699999999999999< Span style= "COLOR: #008000" >#NO2: List resolution:>>> min ([item[p] for Item in books for p in item if p== "price ' ]) 23.699999999999999
#Python Related book SearchFor itemInchBooksFor PInchItemIf item[‘Name'].find (‘Python') >=0:print item[p], #24.7 Python core programming when 55.7 in-depth python Xinhua bookstore total = [Item[p] for Item Span style= "COLOR: #0000ff" >in books for p in Item if item[ "name"].find ( ' python ") >=0]print Total Span style= "COLOR: #008000" >#[24.699999999999999, U ' python\u6838\u5fc3\u7f16\u7a0b ', U ' \u5f53\u5f53 ', 55.700000000000003, U ' \u6df1\u5165python ', U ' \u65b0\u534e\u4e66\u5e97 ']
Second, generator expression
1. Why should I use it?
2. What is the difference between it and list parsing?
"Turn" Python Learning (12)-List parsing