description
filter () function is a built-in function for filtering a queue, filtered ineligible element returns a list of qualified elements.
grammar
filter(function, iterable)
parameter
- function: function judgment
- iterable: iterables
return value
Back to list
note
python2.x is returned in a filtered list, python3 returned is a filter class. filter class implements iter and next method can be seen as an iterator has inert properties calculation, the relative Python2.x improve performance, can save memory. So for the different versions of python compiler you need to pay attention to the follow-up treatment.
Examples
#!/usr/bin/python3
# 过滤列表中所有奇数
def is_odd(n):
return n % 2 == 1
tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
newlist = list(tmplist)
print(newlist)