Everything is a process from quantitative change to qualitative change , Study Python No exception . Only to know the common functions in a language like the back of your hand , To be able to handle the problem with ease , Find the best solution quickly .
This article explores with you Python Medium filter function , Let you understand the principle of this function in the shortest time . You can also use fragmented time to consolidate this function , Make you more efficient in your work .
filter The function is Python Built in functions commonly used in , Call without loading the Library , It can be used directly . It is mainly used to filter elements that do not meet the conditions in the iterator according to specific conditions , Returns a lazy computed filter Object or iterator . Need to use list Function transformation , To get a new list of qualified elements .
The basic calling syntax is as follows :
filter(function or None, iterable)
function: function , The effect is right. iterable Each element in determines whether a particular condition is met .
None: Do not call any function , Only the elements in the iteratable object are judged to be true or false , Elements that remain true .
iterables: Iteratable object ( Sequence 、 Dictionary, etc ).
So let's see filter Function without list Result , The code is as follows :
c = [-10, 28, 9, -5, 30, 5]
filter(lambda a:a>0, c)
Get the results :
<filter at 0x27950dbb9d0>
Returns a lazy computed filter Object or iterator . Next, let's take a look at list Function to get what , The code is as follows :
c = [-10, 28, 9, -5, 30, 5]
list(filter(lambda a:a>0, c))
Get the results :
[28, 9, 30, 5]
Know from the result ,filter The function picks out the sequence c Greater than 0 The elements of form a new object or iterator . adopt list Function transformation , Then we get a new list of qualified elements . If a friend is interested in lambda Function unfamiliar , You can refer to 【Python Common functions 】 One article gives you a thorough grasp of Python Medium lambda function .
stay filter The function definition mentions ,filter Parameters in function None Means that no function is called , Only the elements in the iteratable object are judged to be true or false , Elements that remain true .
The test code is as follows :
# Find the non in the sequence 0 Count
c2 = [4, 9, 0, -5, -8, 7, 0]
list(filter(None, c2))
Get the results :
[4, 9, -5, -8, 7]
because 0 stay Python China and Murdoch think False, Not 0 The default is True, So in the process of screening 0 Filtered .
stay filter The function definition mentions , It handles iteratable objects , So it includes objects such as lists and dictionaries . The first two examples deal with lists , This example looks at the processing of dictionaries . The code is as follows :
# Find the dictionary that is greater than 2 Key
list(filter(lambda x:x>2, {
1:' Andy ', 2:' Liu shiwen ', 3:' Zhang Jike ', 4:' Wang Ming ', 5:' Liu Ming '}))
Get the results :
[3, 4, 5]
Know from the result ,filter Function to process the dictionary , The object to be filtered is the key of the dictionary , Not the value of the dictionary .
Finally, let's look at a problem that primary schools often encounter , seek 100 Within 3 The multiple of is a positive integer of odd number . The code is as follows :
# seek 100 Inside is both odd and 3 A positive integer that is a multiple of
import numpy as np
list(filter(lambda x:x%2!=0 and x%3==0, np.arange(1, 101)))
Get the results :
[3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99]
among np.arange(1, 101)) Express 1 To 100 The tolerance is 1 Equal difference sequence of .
x%2!=0 Indicates that the number cannot be divided by an integer 2, Is an odd number .
x%3==0 Indicates that the number can be divided by 3, That is to say 3 Multiple .
It can be checked manually , Find out Python The result is correct .
thus ,Python Medium filter The function has been explained , If you want to know more Python The function in , You can go to “ The code of Ali Yiyang ” Official account “ Study Python” Module related articles .
You may be interested in :
use Python Drawing Picasso
use Python Draw a cloud of words
use Python draw 520 Eternal heart
Python Face recognition — You are the only one in my eyes
Python Draw a nice picture of the starry sky ( Aesthetic background )
【Python】 Valentine's Day confession fireworks ( With sound and text )
use Python Medium py2neo Library operation neo4j, Building the association map
Python Romantic confession source collection ( love 、 The roses 、 Photo wall 、 Advertising under the stars )
Use scenario:After python nuit