Home > Backend Development > Python Tutorial > Python built-in function——filter

Python built-in function——filter

黄舟
Release: 2017-01-19 16:44:02
Original
1708 people have browsed it

Python built-in function - filter

##filter

filterfilter(function, iterable)
Copy after login

This function is used to traverse all elements from an iterable object iterable. When each After the elements are given as parameters to the function object, the elements judged to be True are retained, while the elements judged to be False are skipped, which is to achieve the goal of filtering unnecessary elements. The parameter iterable is an iterable 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 a value. If this parameter is empty, the identity function will be used as the default function by default.

When function is non-empty, it is equivalent to generating an expression:

item for item in iterable if function(item)) 
当function为空时,相当于生成表达式:
item for item in iterable if item
>>> l = list(filter(None,[0,1,2,3]))
>>> l
[1, 2, 3]
>>> l = list(filter(lambda x: x>5,range(10)))
>>> l
[6, 7, 8, 9]
Copy after login

The above is the content of Python's built-in function - filter. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template