Help on built-in function filter in module __builtin__:
Filter (...)
Filter (function or None, sequence), list, tuple, or string
Return those items of sequence for which function (item) is true. If
function is None, and return the items is true. If sequence is a tuple
or string, return the same type, else return a list.
Filter (function, iterable)
Construct a list from those elements of iterable for which function returns TRUE. Iterable May is either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also have that type; Otherwise it is always a list. If function is a None, the identity function is assumed, which is, and all elements of iterable that is false is removed.
Note that the filter (function, iterable) is equivalent to [item for item ' in Iterable if function (item)] If function was not Non E and [item for item in Iterable if Item] if function is None.
See Itertools.ifilter () and Itertools.ifilterfalse () for iterator versions of this function, including a variation that fi Lters for elements where the function returns FALSE.
English Description:
The purpose of this function is to extract a sequence of elements in a SEQ that can make Func true. The Func function is a Boolean function called by the filter () function that acts on each element of the SEQ once, filters out the elements that match the criteria, and returns as a list.
>>> nums=[2,3,6,12,15,18]
>>> def nums_res (x):
... return x%2 = = 0 and x%3 = = 0
...
>>> Print filter (nums_res,nums)
[6, 12, 18]
>>> def is_odd (n):
... return N%2==1
...
>>> Filter (is_odd, [1,2,4,5,6,9,10,15])
[1, 5, 9, 15]
>>> def not_empty (s):
... return s and S.strip ()
...
>>> Filter (Not_empty, [' A ', ' ', ' B ', None, ' C ', '])
[' A ', ' B ', ' C ']
This article is from the "Big Cloud Technology" blog, please be sure to keep this source http://hdlptz.blog.51cto.com/12553181/1900107
Python built-in function 5-filter ()