Python standard library: built-in function filter (function, iterable), pythoniterable
This function is used to traverse all elements from an iteration object iterable. When each element is run on the function object as a parameter, the elements that are judged to be True are retained, while those that are False are skipped, that is, to filter unnecessary elements. The iterable parameter is an iteratable object, such as a list, Dictionary, string, or function object with an iterator. The Parameter function is a function that can input elements for judgment and return values. If this parameter is null, the identity function is used by default as the default function.
When the function is not empty, it is equivalent to generating an expression:
Item for item in iterable if function (item ))
When the function is empty, it is equivalent to generating an expression:
Item for item in iterable if item
Example:
# Filter () l = list (filter (None, [0, 1, 2, 3]) print (l) a = [1, 2, 3, 4, 5, 6, 7] B = list (filter (lambda x: x> 5, a) print (B)
The output result is as follows:
[1, 2, 3]
[6, 7]
Cai junsheng QQ: 9073204 Shenzhen