Powerful and elegant list derivation expression in python

Posted by tinuviel on Thu, 02 Apr 2020 21:38:33 +0200

The derivation expression is actually to simplify some loop judgment operations, etc

How many ways can you generate a list of numbers 1-10?

>>> l = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 
>>> l = []
>>> for x in range( 1, 11 ):
...     l.append( x )
... 
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>> l = range( 1, 11 )
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 

Now let's look at the derivation

>>> a = [ x for x in range( 1, 11 ) ]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 

Some people may say that it's better to directly range (1, 11). It's unnecessary. If we want to filter out odd numbers?

Of course, range can still:

>>> range( 1, 11, 2 )
[1, 3, 5, 7, 9]
>>> 

So, if we want to get even numbers, we need to go through every term, judge

>>> a = []
>>> for x in range( 1, 11 ):
...     if x % 2 == 0:
...             a.append( x )
... 
>>> 
>>> a
[2, 4, 6, 8, 10]
>>> 

He is equivalent to the following derivation:

>>> b = [ x for x in range( 1, 11 ) if x % 2 == 0 ]
>>> b
[2, 4, 6, 8, 10]
>>> 

In a word

Generate a coordinate system?  

>>> dot = [(x,y) for x in range( 1, 10 ) for y in range( 1, 10 ) ]
>>> dot
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
>>> 

Equivalent to the following 2-fold cycle:

>>> dot = []
>>> for x in range( 1, 10 ):
...     for y in range( 1, 10 ):
...             dot.append( ( x, y ) )
... 
>>> dot
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
>>> 

Generate an incremental string list:

>>> ['the number:%s' % n for n in range( 1, 10 ) ]
['the number:1', 'the number:2', 'the number:3', 'the number:4', 'the number:5', 'the number:6', 'the number:7', 'the number:8', 'the number:9']

Find the square of 1-9

>>> [x ** 2 for x in range( 1, 10 ) ]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> 

Use the dictionary to package a layer. The same key will cover the previous one

>>> dict( [( x, y ) for x in range( 1, 5 ) for y in range( 1, 5 )] )
{1: 4, 2: 4, 3: 4, 4: 4}
>>> [( x, y ) for x in range( 1, 5 ) for y in range( 1, 5 )]
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]

 

List reference, and javascript type:

>>> l = ['my', 'name', 'is', 'ghostwu' ]
>>> l
['my', 'name', 'is', 'ghostwu']
>>> a = l
>>> a[3] = 'wukong'
>>> l
['my', 'name', 'is', 'wukong']
>>> del a
>>> l
['my', 'name', 'is', 'wukong']
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> 

del a, delete the list reference, similar to the php garbage collection mechanism, two variables point to a list, delete one, but the other still points to that list

>>> a = [10, 20, 30 ]
>>> b = a
>>> b
[10, 20, 30]
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> b
[10, 20, 30]
>>> 

del a [], which means clear list

>>> a = [ 10, 20, 30 ]
>>> b = a
>>> del a[:]
>>> 
>>> a
[]
>>> b
[]
>>> 

Topics: Python Javascript PHP