One, List analytic type
List parsing is a pair of brackets outside, which returns a list.
The general form is: [Expr for item ' in itratoble]
Print ([i+1 for I in Range]) #结果: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
DEF Inc (X): return X**2print ([Inc (I) for I in Range]) #结果: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print ([I for I in range] if i%2==0]) #结果: [0, 2, 4, 6, 8]
The filter () method takes a function and an iterator object to two parameters, returning a generator object.
List (filter (lambda x:x%2==0,range)) #结果: [0,2,4,6,8]
The map () method takes a function and an iterative object to two parameters, returning a list.
Map (Lambda X:x+1,range) #结果: [1,2,3,4,5,6,,7,8,9,10]
List parsing executes faster than filter () and map ().
Second, generator analytic type
The generator parsing is a pair of parentheses outside, which returns a generator object. It is calculated on demand or delayed.
The general form is: (Expr for item in itratoble)
(X for X in range (ten) if x%2==0)
Three, set analytic type
The set parsing is a pair of curly braces outside, which returns a collection, and it has all the characteristics of the collection.
The general form is: {Expr for item ' in hash-able object}
{x for x in range} #结果: {0,1,2,3,4,5,6,7,8,9}
Four, dictionary analytic style
The dictionary parsing is also a pair of curly braces outside, it requires two expressions, one generates a key, the other generates a value, and two expressions are separated by colons. So it returns a dictionary.
The general form is: {k:v for item in Itratoble}
Python-parsed