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

Python built-in function——filter

黄舟
黄舟Original
2017-01-19 16:44:021679browse

Python built-in function - filter

##filter

filterfilter(function, iterable)

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]

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)!

Statement:
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