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