List Parsing
Let's take a look at the following example, which is to get the square of each integer from 1 to 9 and print the result in the list
>>> Power2 = []>>> for i in Range (1,10): ... Power2.append (i*i) ... >>> power2[1, 4, 9, 16, 25, 36, 49, 64, 81]
Python has a very interesting function, that is, list parsing, that is:
>>> squares = [x**2 for x in range (1,10)]>>> squares[1, 4, 9, 16, 25, 36, 49, 64, 81]
Crossing not surprised to see this result? This is Python, the pursuit of simple and elegant python!
In its official documentation, there is a description of the meaning of List parsing:
List Comprehensions provide a concise to create lists. Common applications is to make new lists where each element is the result of some operations applied to each member of an Other sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
Do you remember that question in the previous lecture?
Find the positive integers within 100 that can be divisible by 3.
The method we use is:
aliquot = []for N in range (1,100): if n%3 = = 0: aliquot.append (n) Print aliquot
All right. Now with the list parsing rewrite, this will be the case:
>>> aliquot = [n for n in range (1,100) if n%3==0]>>> aliquot[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 , 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
Shook it. Absolute Bull X!
In fact, not only the number of the list, all can do so. Please, after calming the excited heart, look silently at the following code, feeling the charm of the list parsing.
>>> mybag = [' Glass ', ' apple ', ' green leaf '] #有的前面有空格, some have spaces behind >>> [One.strip () for one in mybag]
#去掉元素前后的空格 [' Glass ', ' apple ', ' green leaf ']enumerate
This is an interesting built-in function where we can get the number of each element of a list by means of the for I in range (Len list), and then get the element in the way of List[i]. What if I want to get the element number and element at the same time? That's it:
>>> for I in range (Len (week)): ... . Print week[i]+ ' is ' +str (i) #注意, I is of type int, if and previous with + connection, must be str type ... Monday is 0sunday is 1friday is 2
Python provides a built-in function enumerate that enables similar functionality
>>> for (I,day) in Enumerate (week): ... Print day+ ' is ' +str (i) ... Monday is 0sunday-1friday is 2
is an interesting built-in function, mainly to provide a simple and quick method.
This is what the official document says:
The code is as follows:
Return an Enumerate object. Sequence must is a sequence, an iterator, or some other object which supports iteration. The next () method of the iterator returned by enumerate () returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating through sequence:
By the way to transcribe a few examples, for crossing appreciation, it is best to experiment.
>>> seasons = [' Spring ', ' Summer ', ' Fall ', ' Winter ']>>> list (enumerate (seasons)) [(0, ' Spring '), (1, ' Summer '), (2, ' Fall '), (3, ' Winter ')]>>> list (Enumerate (seasons, Start=1)) [(1, ' Spring '), (2, ' Summer '), (3, ' Fa ll '), (4, ' Winter ')]
Here is something like (0, ' Spring '), which is another type of data to be explained later.