程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python concisely explains the usage of filter function

編輯:Python

Catalog

One 、filter Definition of function

Two 、filter Function instance

Find a new sequence composed of elements greater than zero in the sequence

Find a new sequence composed of nonzero numbers in the sequence

Find greater than in the dictionary 2 A new sequence of bonds of

seek 100 Within 3 The multiple of is a positive integer of odd number

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 .

One 、filter Definition of function

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

Two 、filter Function instance Find a new sequence composed of elements greater than zero in the sequence

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 .

Find a new sequence composed of nonzero numbers in the sequence

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 .

Find greater than in the dictionary 2 A new sequence of bonds of

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 .

seek 100 Within 3 The multiple of is a positive integer of odd number

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 nplist(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 .

This is about Python Brief explanation filter This is the end of the article on the use of functions , More about Python filter Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved