Python built-in function 5-filter ()

Source: Internet
Author: User
Tags iterable

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.