This article allows you to thoroughly grasp [filter function in Python]

Everything is a process from quantitative change to qualitative change, and learning Python is no exception. Only by knowing the common functions in a language well, can we be handy in the process of dealing with problems and quickly find the optimal solution.
  
This article will explore the filter function in Python with you, so that you can understand the principle of this function in the shortest time. You can also use the fragmented time to consolidate this function to make you more efficient in the process of processing.
  
insert image description here
  
  

1. Definition of filter function

  
The filter function is a commonly used built-in function in Python, which can be used directly without loading a library. It is mainly used to filter unqualified elements in the iterator according to specific conditions, and returns a filter object or iterator for lazy calculation. It needs to be converted with the list function to get a new list of elements that meet the conditions.
  
Its basic call syntax is as follows:

filter(function or None, iterable)

function: function, the function is to judge whether each element in the iterable meets a specific condition.
  
None: Do not call any function, only judge true or false for the element in the iterable object itself, and keep the true element.
  
iterables: Iterable objects (sequences, dictionaries, etc.).

  
  

Two, filter function example

  

Example 1: Find a new sequence composed of elements greater than zero in a sequence

  
Let's first look at the result of the filter function without adding a list. The code is as follows:

c = [-10, 28, 9, -5, 30, 5]
filter(lambda a:a>0, c)

got the answer:

<filter at 0x27950dbb9d0>

Returns a lazily evaluated filter object or iterator. Next, let's see what you get when you use the list function to convert. The code is as follows:

c = [-10, 28, 9, -5, 30, 5]
list(filter(lambda a:a>0, c))

got the answer:

[28, 9, 30, 5]

From the results, the function of the filter function is to select elements greater than 0 in the sequence c to form a new object or iterator. Convert through the list function, and then get a new list composed of eligible elements. If a friend is not familiar with the lambda function in the code, you can refer to the article [Python Common Functions] to let you thoroughly grasp the lambda function in Python.

  

Example 2: Find a new sequence composed of non-zero numbers in the sequence

  
As mentioned in the definition of the filter function, the parameter None in the filter function means that no function is called, and only the elements in the iterable object are judged true or false, and the true elements are kept.
  
The test code is as follows:

#找出序列中的非0数
c2 = [4, 9, 0, -5, -8, 7, 0]
list(filter(None, c2))

got the answer:

[4, 9, -5, -8, 7]

Since 0 defaults to False in Python, and non-zero defaults to True, 0 is filtered during the filtering process.

  

Example 3: Find a new sequence composed of keys greater than 2 in the dictionary

  
As mentioned in the filter function definition, it processes iterable objects, so it includes objects such as lists and dictionaries. The first two examples deal with lists. In this example, look at the treatment of dictionaries. code show as below:

#找出字典中大于2的键
list(filter(lambda x:x>2, {
    
    1:'杨紫', 2:'刘诗雯', 3:'张继科', 4:'王明', 5:'刘明'}))

got the answer:

[3, 4, 5]

From the results, when the filter function processes the dictionary, the filtered object is the key of the dictionary, not the value of the dictionary.

  

Example 4: Find positive integers within 100 that are both multiples of 3 and odd

  
Finally, let's look at a question that is often encountered in elementary school, which is to find positive integers that are multiples of 3 and odd numbers within 100. code show as below:

#求100以内既是奇数又是3的倍数的正整数
import numpy as np
list(filter(lambda x:x%2!=0 and x%3==0, np.arange(1, 101)))

got the answer:

[3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99]

Among them, np.arange(1, 101)) represents an arithmetic sequence with a tolerance of 1 from 1 to 100.
  
x%2!=0 means that the number cannot be divided by 2, which is an odd number.
  
x%3==0 means that the number can be divided by 3, which is a multiple of 3.
  
You can check it manually and find that the result obtained by Python is correct.
  
So far, the filter function in Python has been explained. If you want to know more about functions in Python, you can go to the "Learning Python" module related articles in the "Ali Yiyang's Code" official account.

  
You may be interested in:
Draw Pikachu with Python
Draw a word cloud map
with Python Draw 520 eternal heart beats with Python Python face recognition - you are the only one
in my eyes With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Long press (scan) to recognize the QR code above to learn more Python and modeling knowledge, making your study and work more brilliant.

Guess you like

Origin blog.csdn.net/qq_32532663/article/details/125460944