Higher-order functions map and filter small ape circle of usage python

python has many built-in functions, built-in functions encapsulates many features that make us very convenient to use, small circle ape detailed explanation video, my friends can go to look for higher-order functions, small series summed up after school, said the following about the use of higher-order functions map and filter.
. 1, filter () function
filter function accepts two arguments, a function, a is the sequence. filter () function is passed the sequentially applied to each element, and based on the return value is True or False decided to keep or discard the element having a screening effect.
Note: 3.x returns a filter object (map function also returns a subsequent object), add to list () and other types of conversion; the previous version 2.x return value and parameters of the same type of sequence type.
Example:

DEF is_oushu (I):
     IF (I% 2 == 0):
     return True
 the else :
     return False 
L = [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10 ] 
L = filter (is_oushu , L)
 Print (L) # 3.x Object returns the object filter 
L = List (L) # 3.x type conversion to be done 
Print (L)

2, map function

map () function accepts two arguments, a function, a subject is an iterative (Iterable), map the incoming function sequentially applied to each element of the sequence, and the result as a new iteration of the object may be returned.
Example:

. 1 
2 DEF SQR (I): 
. 3 return I ** 2 
. 4 L = [l, 2,3] 
. 5 L = Map (SQR, L) 
. 6 Print (L) # 3.x returns the object Object Map 
. 7 L List = (L) 
. 8 Print (L)

Use filter function and the map function is not very simple, but in practice, the application of these two higher-order functions quite extensive, want in-depth study of little friends, you can go to a small circle ape doing exercises, exercises there are very interesting little game, do some expansion.

Guess you like

Origin www.cnblogs.com/xiaoyuanquan/p/10967364.html