One or three-dollar operation (type)
For the general simple if else condition judgment sentence can be expressed by ternary operation
The specific mode is:
If CONDITION:EXPR1ELSE:EXPR2 equivalent to: expr1 if condition else expr2
Explanation: Executing an ELSE EXPR2 expression if the IF condition condition is true if the EXPR1 expression is executed
Example ①
>>> if 2 = = 2: ... name = ' Cool ' ... else: ... name = ' hot ' ... >>> name = ' Cool ' if 2==2 else ' hot ' ;>> Print namecool>>>
Second, lambda expression:
For simple functions, it can be substituted in another way, that is, lambda
For example, there are the following functions:
>>> def Fun (ARG): ... return arg + arg ... >>> >>> result = Fun (+) >>> print result200>>>
Define a variable F_lambda, assign lambda arg:arg+1 to F_lambda
>>> F_lambda = lambda arg:arg +1>>> result = F_LAMBDA (111) >>> Print result112>>>
You can also use other expressions:
>>> test = lambda a:a**2>>> test_result = Test (3) >>> print test_result9>>>
As you can see from the above example, the expression behind the lambda can be defined arbitrarily, as long as it conforms to the syntax requirements of Python.
Lambda expression:
① for handling simple logic
② will automatically return data
Third, built-in function map
The role of map is to manipulate each element in the sequence and then output a new sequence
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/77/DD/wKiom1Zv323R8pKhAABLX8BNk2A769.png "title=" Map.png "alt=" Wkiom1zv323r8pkhaablx8bnk2a769.png "/>
>>> NUM1 = [10,9,8,7,6]>>> num2 = map (lambda a:a**2,num1) >>> print num2[100, Bayi, +, $, 36]> ;>> >>> num3 = [1,2,3,4,5]>>> num4 = map (lambda a,b:a-b,num1,num3) >>> print num4[9, 7, 5 , 3, 1]>>>
Or
>>> num = [12,33,55,85]>>> def func (ARG): ... return arg + 10 ... >>> new_num = map (func,num) >>> print new_num[22, +, 95]>>>
The above examples are explained in essence as follows:
>>> new_num = []>>> for item in num: ... new_item = Item + 10 ... New_num.append (new_item) ... >>> >>> print new_num[22, 43, 65, 95]
Iv. built-in function filter
Filter works by filtering out the conditions in the sequence and then forming a new sequence
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/77/DC/wKioL1Zv4LDiFtx1AABLGudR8bc619.png "title=" Filte.png "alt=" Wkiol1zv4ldiftx1aablgudr8bc619.png "/>
>>> NUM1 = [10,9,8,7,6]>>> tmp = filter (lambda arg:arg >5,num1) >>> print tmp[10, 9, 8, 7, 6 ]>>>
Or
>>> TMP2 = filter (lambda n->5,num1) >>> print tmp2[10, 9, 8, 7, 6]>>>
Or
#!/usr/bin/env python# -*- coding:utf8 -*-num = [11,22,0,33]print filter (None,num) [[email protected] day004]# python lam.py [11, 22, 33][[email protected] day004]# [[email protected] Day004]# cat lam.py #!/usr/bin/env python# -*- coding: Utf8 -*-num = [11,22,0,33, "]print filter (None,num) [[email protected] day004]# python lam.py [11, 22, 33][[email protected] day004]# cat lam.py #!/usr/bin/env python# -*- coding:utf8 -*-num = [ 11,22,0,33, "", False]print filter (None,num) [[email protected] day004]# python lam.py   [11, 22, 33]
Summary: From the above can be found, by default, when processing data with filter, filter will be the output of the Boolean value true (generally return the Boolean value of True to the new list, and not return to the new list), false ignored; Of course, the filter can also pass in the function, As in the above example, the lambda statement;
Five. Built-in function reduce
The role of reduce is to manipulate all elements within a sequence
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/77/DE/wKiom1Zv-FeDbhPIAABu1x9ZUok939.png "title=" Reduce.png "alt=" Wkiom1zv-fedbhpiaabu1x9zuok939.png "/>
>>> NUM1 = [10,9,8,7,6] >>> result =reduce (lambda arg1,arg2:arg1+arg2,num1) >>> Print result40>>> NUM5 = [1,2,3,4,5,6] >>> sum = reduce (lambda a,b:a+b,num5) >>> print SUM21&G T;>>
# The first parameter of reduce, the function must have two parameters
# The second parameter of reduce, the sequence to loop
# The third parameter of reduce, the initial value
Six, yield generator
Here are some examples:
>>> Print Range (8) [0, 1, 2, 3, 4, 5, 6, 7]>>> print xrange (8) xrange (8) >>>
As you can see from above, range can generate a list that creates the specified number in memory, while Xrange does not, and then looks down:
>>> for N in xrange (8): ... print n ... 01234567>>>
Xrange only creates numbers when the loop is in progress, that is, when the iterations are created;
This article from "Flat Light is true" blog, please be sure to keep this source http://cryan.blog.51cto.com/10837891/1726976
Ternary, lambda, built-in function map, reduce, filter, and yield generators