A detailed description of the built-in function filter in Python

Source: Internet
Author: User
Tags iterable
This article mainly describes the Python built-in function filter related materials, the need for friends can refer to the following

Python built-in function filter

Class Filter (object): "" "Filter (function or None, iterable)--Filter Object  Return an iterator yielding those it EMS of iterable for which function (item) is true. If function is None, and return the items is true. """

Filter (Func,iterator)

Func: The resulting value in a custom or anonymous function is a Boolean value, True preserves the value taken by the function, and false is reversed.
Iterator: The object can be iterated.

Cases:

Filter list [' Text_test_text ', ' test_text_1 ', ' text_test_2 ', ' 3_test_text ', ' test_test ']
As long as it contains the text string and takes it out or reversed.

S.rfind ' text ' +1

Python3 RFind () returns the last occurrence of the string, or 1 if there is no match.
The number 0 is false,0 above the integer is true, so s.rfind ' text ' will be +1, no characters and -1+1=0 found.

# Filter

Li = [' Text_test_text ', ' test_text_1 ', ' text_test_2 ', ' 3_test_text ', ' test_test ']# the value that the default reserved function takes to print (list ( Lambda s:s.rfind (' text ') + 1, li)) # Inverse, the next three examples are the same print (list (filter (lambda s:not s.rfind (' text ') + 1, Li)))

# Noe Custom Functions

L1 = [' Text_test_text ', ' test_text_1 ', ' text_test_2 ', ' 3_test_text ', ' test_test ']def distinguish (l): nl = [] for s in l:
  if s.rfind ("text") + 1:   nl.append (s) return Nlprint (distinguish (L1))

# both custom high-order functions

L2 = [' Text_test_text ', ' test_text_1 ', ' text_test_2 ', ' 3_test_text ', ' Test_test ']def f (s): Return S.rfind (' text ') + 1def D Istinguish (func, array): NL = [] for s in array:  if Func (s):   nl.append (s) return Nlprint (distinguish (f, L2))

# three anonymous function

l3 = [' Text_test_text ', ' test_text_1 ', ' text_test_2 ', ' 3_test_text ', ' test_test ']def Distinguish (func, array): NL = [] for s in array:if func (s): Nl.append (s) return nlprint (distinguish (lambda s:s.rfind (' text ') + 1, L3)) 
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.