Python sequence (8) list derivation real column

Time:2020-11-7

1. List derivation list derivation can form a new list very succinctly: only one simple expression can transform and transform the obtained elements.

Format: [expression for variable in list] or [expression for variable in list if condition]

Filter conditions are optional, depending on the actual application, leaving only the expression

>>> list =[x*x for x in range(10)]
amount to
>>> alist= []
>>> for x in range(10):
也amount to
>>> list= list(map(lambda x:x*x,range(10)))

2. Afanti played chess with the king, and the king said that if izji lost, he could take whatever Avanti wanted. Afanti said that the order of rice, chessboard a total of 64 small grid, in the third grid put four grains of rice, the fourth grid put eight grains of rice, and so on, the back of each grid is twice as much as the previous grid, has been full of 64 grids. How many grains of rice do you need?

>>> sum([2**i for i in range(64)])
18446744073709551615

3. Using list derivation to realize the tiling of nested list

>>> vec=[[1,2,3],[4,5,6],[7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9] is equivalent to
>>> vec=[[1,2,3],[4,5,6],[7,8,9]]
>>> result =[]
>>> for elem in vec:
      for num in elem:
      result.append(num)
>>> result
[1,2,3,4,5,6,7,8,9]

4. If you don’t use list derivation, you can do this.

>>> vec=[[1,2,3],[4,5,6],[7,8,9]]
>>> sum(vec,[])
[1, 2, 3, 4, 5, 6, 7, 8, 9] or > > > VEC = [[1,2,3], [4,5,6], [7,8,9]] > > from itertools import chain > > > list (chain (* VEC)) [1, 2, 3, 4, 5, 6, 7, 8, 9]

5. List all Python source files in the current folder

 

>>> import os
>>> [filename for filename in  os.listdir ('.')if  filename.endswith (('. Py', '. PYW'))] filter unqualified elements > > > List = [- 1, - 4,6,7,5, - 2,3,9, - 11] > > > > > > [i for I in list if I > 0] [6, 7, 5, 3, 9]

6. It is known that there is a dictionary containing the scores of some students. Calculate the highest score, lowest score and average score of the score, and find all the students with the highest score.

 

>>> scores={"zhangsan":45,"lisi":78,"wangwu":40,"zhouliu":96,"zhaoqi":65,"sunba":90,"zhengjiu":78,"wushi":99,"dongshiyi":60}
>>> heighest=max(scores.values())
>>> lowest = min(scores.values())>>> average=sum(scores.values())*1.0/len(scores)
>>> heighest,lowest,average
(99, 40, 72.33333333333333)
>>> heighestPerson=[name for name,score in scores.items()if score == heighest]
>>> heighestPerson
['wushi']

7. Use multiple loops in the list derivation to realize the combination of multiple sequence elements, and filter specific elements with conditional statements

>>> [(x,y)for x in range(3) for y in range(2)]
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
>>> [(x,y) for x in [1,2,3]for y in [3,1,4]if x!=y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

 

 

 

 

 

 

 

 

 

Recommended Today

[Q & A share the second bullet] MySQL search engine, after watching the tyrannical interviewer!

Hello, I’m younger brother. A few days ago, I shared the second interview question, the interview site of search engine in MySQL. This question is the interview at normal temperature. After reading it, I’m sure you will gain something in terms of database engine If you haven’t read my first share, you can refer to […]