Python's filter function screening method implementation

filter screening method can be implemented, the first parameter is a function, the return value is True or False, the second parameter may be STR, tuple, List, the back of the parameters passed to the function in turn, turn the determination result, the result is to leave True's. such as:

d = filter(lambda x: x%2, [1,2,3,4])

The result is 1,3 implementation process:

1, the list 1 passed to the function in front of x, determination result whether the x% 2 True (non- 0), True on the left

2, the list 2 to the transfer function of x, determination result whether the x% 2 True (non- 0), True on the left

3, and so on until all judging is completed

4, the result is: [ 1,3]

 

 

Look the following, the first parameter is None, this time all values are returned back,

e = filter(None, "I love python")
print(list(e))

 

The results print as follows:

['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n']

 

Guess you like

Origin www.cnblogs.com/sy-zxr/p/12054073.html