You can often see assignment statements in the form of ret = [x ** 2 for x in lst]
in python. It is not easy for people who switch from C++ to python to understand the use of this for loop. This is python New syntax invented for brevity. Python analytical formula has the following advantages:
Python's analytical formula is divided into the following four types:
The following describes the use of these four analytical formulas.
List comprehensive form
[ expr for e in iterator]
In [1]: lst =range(10)
In [2]:%%timeit
...: ret =[x **2for x in lst]...:100000 loops, best of3:5.28 µs per loop
In [3]:%%timeit
...: ret =[]...:for x in lst:...: ret.append(x **2)...:100000 loops, best of3:6.09 µs per loop #Slightly more time-consuming
It can be found that the efficiency is slightly higher, and the most important thing is the simplicity of the code.
List comprehensions can be used with if
statements
For example, filter out the even numbers in the list lst
:
In [4]: ret =[]
In [5]:for x in lst:...:if x %2==0:...: ret.append(x) #Use for loop
...:
In [6]: ret
Out[6]:[0,2,4,6,8]
In [7]: ret =[x for x in lst if x %2==0] #Use list comprehensions
In [8]: ret
Out[8]:[0,2,4,6,8]
List comprehensions can use if
statements like for
loops.
List comprehensive for
statements can be nested.
In [9]:(x, y)for x inrange(0,5)for y inrange(5,10)
File "<ipython-input-9-825e2443da8b>", line 1(x, y)for x inrange(0,5)for y inrange(5,10)^
SyntaxError: invalid syntax
# Explain that list comprehensions must be enclosed in square brackets
In [10]:[(x, y)for x inrange(5)for y inrange(5,10)]
Out[10]:[(0,5),(0,6),(0,7),(0,8),(0,9),(1,5),(1,6),(1,7),(1,8),(1,9),(2,5),(2,6),(2,7),(2,8),(2,9),(3,5),(3,6),(3,7),(3,8),(3,9),(4,5),(4,6),(4,7),(4,8),(4,9)]
In [11]: ret =[]
In [12]:for x inrange(5):...:for y inrange(5,10):...: ret.append((x, y))...:
In [13]: ret
Out[13]:[(0,5),(0,6),(0,7),(0,8),(0,9),(1,5),(1,6),(1,7),(1,8),(1,9),(2,5),(2,6),(2,7),(2,8),(2,9),(3,5),(3,6),(3,7),(3,8),(3,9),(4,5),(4,6),(4,7),(4,8),(4,9)]
Special usage of if
statement
The single-line if statement is very similar to the list comprehension.
Expression form: x if cond else y
if
and else
must exist at the same time.
Let’s take an even number to square and an odd number to cube as an example to demonstrate
In [14]: ret =[]
In [15]:for x in lst:...:if x %2==0:...: ret.append(x **2)...:else:...: ret.append(x **3)...:
In [16]: ret
Out[16]:[0,1,4,27,16,125,36,343,64,729]
In [17]: x =3
# if special usage
In [18]: x **2if x %2==0else x **3
Out[18]:27
In [19]:3if True else4
Out[19]:3
# If special usage of if with list comprehension x if cond else y for...
In [20]:[x **2if x %2==0else x **3for x in lst]
Out[20]:[0,1,4,27,16,125,36,343,64,729]
List comprehensions return a list, and generator comprehensions return a parsing. The brackets of the list comprehensions become parentheses, which is the generator parsing
In [1]:range(10000)
Out[1]:range(0,10000)
In [2]: g =(x **2for x inrange(100000000000))
In [3]: g
Out[3]:<generator object <genexpr> at 0x7f9f08a5f0a0>
In [4]:next(g)
Out[4]:0
In [5]:next(g)
Out[5]:1
In [6]:next(g)
Out[6]:4
Choice of List Comprehension and Generator Comprehension
Replacing the square brackets of the list comprehension with curly braces is the set comprehension.
In [1]: lst =[2,4,5,6,3,4,2]
In [2]: s ={x for x in lst}
In [3]: s
Out[3]:{2,3,4,5,6} #It can be seen that the repetition will be removed when the list comprehension is generated, which meets the requirements of the collection
In [4]:type(s)
Out[4]:set
The dictionary analysis also uses curly braces, but unlike the set analysis, the use of expr
is not a single element but a k,v
pair.
In [1]:{str(x): x for x inrange(5)}
Out[1]:{'0':0,'1':1,'2':2,'3':3,'4':4}
The most widely used of the four analytical formulas is the list comprehension, which often has some clever usages.
Recommended Posts