Python filter usage and pythonfilter usage

Source: Internet
Author: User
Tags iterable

Python filter usage and pythonfilter usage

1 class filter (object) 2 | filter (function or None, iterable) --> filter object3 | 4 | Return an iterator yielding those items of iterable for which function (item) 5 | is true. if function is None, return the items that are true.

Filter reads all iterable items, determines whether these items are true for the function, and returns an iterator containing all the real items. If the function is None, a non-empty item is returned.

1 In [2]: import re2 In [3]: I = re. split (',', "123, 123213, 123213,") 3 In [4]: i4 Out [4]: ['20140901 ','', '123', '','', '123', '']

In this case, list I contains an empty string.

1 In [7]: print (* filter (None, I) 2 123 123213 123213

In this case, the filter filters out the empty strings in the list to obtain an iterator containing only non-empty strings.

In [9]: print (list (filter (lambda x: x = '', I) ['', '']

Since lambda is true for empty strings, filter filters out non-empty strings, leaving only empty strings.

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.