How to use Python's filter function

Introduction

Python's built-in filter() function can filter certain elements from iterable objects (such as dictionaries, lists) and generate a new iterator. An iterable object is a Python object that can be "traversed", that is, it will return the elements in order so that we can use it in the for loop.

The basic syntax of filter() function is:

filter(function, iterable)

Return an iterable filter object, which can be converted into a list using the list() function. This list contains all the items returned in the filter object.

The filtering method provided by the filter() function is usually more effective than using list comprehensions, especially when we are dealing with larger data sets. For example, a list comprehension will generate a new list, which will increase the running time of the process. When the list comprehension finishes executing its expression, there will be two lists in memory. However, filter() will generate a simple object that contains a reference to the original list, the provided function, and the index of the position in the original list, so the operation takes up less memory.

Here are four different uses of filter():

Use special functions in filter()

The first parameter of filter() is a function, which is used to determine the leaving and leaving of each item in the iterable object referenced by the second parameter. After this function is called, when False is returned, the corresponding value in the iterable object in the second parameter will be deleted. For this function, it can be a normal function or a lambda function, especially when the expression is not so complicated.

Here is how to use the lambda function in filter():

filter(lambda item: item[] expression, iterable)

Use the following list for the lambda function to filter the elements in the list according to the expression of the lambda function.

creature_names =['Sammy','Ashley','Jo','Olly','Jackie','Charlie']

To filter the names of aquarium creatures that start with a vowel in this list, the lambda function is as follows:

print(list(filter(lambda x: x[0].lower()in'aeiou', creature_names)))

Here, we declare an item in the list as x, and access the first character of each string in the manner of x[0], and convert the letter to lowercase to ensure that the letter and The characters in 'aeiou' match.

Finally, provide an iterable object creature_name. As in the previous section, use list() to convert the returned result into a list table.

The output is as follows:

[' Ashley','Olly']

Of course, writing a function can achieve similar results:

creature_names =['Sammy','Ashley','Jo','Olly','Jackie','Charlie']

def names_vowels(x):return x[0].lower()in'aeiou'

filtered_names =filter(names_vowels, creature_names)print(list(filtered_names))

An expression is used in the names_vowels function to complete the filtering of creature_names.

Similarly, the output is as follows:

[' Ashley','Olly']

In general, the result of using the lambda function in the filter() function is the same as the result obtained by using a regular function. If the data to be filtered is more complicated, you may also want to use regular expressions, which may improve the readability of the code.

Use None in filter()

We can also use None as the first parameter of filter() to let the iterator filter out objects whose boolean value is False in Python, such as objects with a length of 0 (such as empty lists or empty strings) Or an object that is numerically equal to 0.

In the following example, you want to filter a list and remove elements whose boolean value is False.

aquarium_tanks =[11, False,18,21,"",12,34,0,[],{}]

filtered_tanks =filter(None, aquarium_tanks)

This code uses None in filter() and passes the aquarium_tanks list as an iterable item. With None as the first parameter, you can check whether the elements in the list are False.

print(list(filtered_tanks))

Then pass filtered_tanks to the list() function, so that you get a list.

From the output result, we can see that we got the integer we wanted, and those items whose boolean value is False are filtered out.

[11,25,18,21,12,34]

Note: If you do not use list() and print filtered_tanks, you will get a similar to <filter object at 0x7fafd5903240> Such a filter object. The filter object is iterable, so we can use a for loop to convert it, or use list() to convert it to a list.

With None, use filter() to quickly delete items considered to be False from the list.

Use filter() for complex scenes

For complex data structures, filter() can also do the job. For example, if there is a list of dictionaries, we not only need to traverse each item (dictionary) in the list, but also every key-value pair in the dictionary. In order to get all the data.

For example, suppose we have a list of each type of creature in the aquarium and the different details of each type of creature. Use the following list to display this data.

aquarium_creatures =[{"name":"sammy","species":"shark","tank number":"11","type":"fish"},{"name":"ashley","species":"crab","tank number":"25","type":"shellfish"},{"name":"jo","species":"guppy","tank number":"18","type":"fish"},{"name":"jackie","species":"lobster","tank number":"21","type":"shellfish"},{"name":"charlie","species":"clownfish","tank number":"12","type":"fish"},{"name":"olly","species":"green turtle","tank number":"34","type":"turtle"}]

Next, write a function to filter the data. In order for filter() to access every dictionary and every element in the dictionary, this requires constructing a nested function as follows:

def filter_set(aquarium_creatures, search_string):
 def iterator_func(x):for v in x.values():if search_string in v:return True
  return False
 returnfilter(iterator_func, aquarium_creatures)

Define the filter_set() function with aquarium_creatures and search_string as parameters. In filter_set(), use the internal function iterator_func() as the parameter of filter(). The filter_set() function will return the iterator generated by filter().

iterator_func() takes x as a parameter, which represents an item in the list (ie a single dictionary).

Next, for loops through each key-value pair in the dictionary, and then uses a conditional statement to check that search_string is the value in the key-value pair.

The iterator_func function is used as the parameter object of the filter function to filter the iteration object. For example: Use filter_set() to search for a string:

filtered_records =filter_set(aquarium_creatures,"2")

Once the function is executed, the filter [object storage] (https://cloud.tencent.com/product/cos?from=10680) is in the filtered_records variable, we convert it to a list and print it:

print(list(filtered_records))

Output content:

[{' name':'ashley','species':'crab','tank number':'25','type':'shellfish'},{'name':'jackie','species':'lobster','tank number':'21','type':'shellfish'},{'name':'charlie','species':'clownfish','tank number':'12','type':'fish'}]

In the previous example, we used filter() to filter the specified characters in the list of dictionaries.

in conclusion

This article lists the different ways of using the filter() function. If you intend to learn more, please read the book "Python University Practical Course" (Electronic Industry Press), which is a rare reading for zero-start readers with special emphasis on engineering practice.

Recommended Posts

How to use Python&#39;s filter function
How to use python&#39;s help function
How to use the round function in python
How to use the zip function in Python
How to use the format function in python
How to use python tuples
How to use hanlp in ubuntu
How to use python thread pool
How to filter numbers in python
How to use SQLite in Python
How to use and and or in Python
How to use PYTHON to crawl news articles
How to run id function in python
How to use Samba server on Ubuntu 16.04
How to use Prometheus to monitor your Ubuntu 14.04 server
How to configure TensorFlow use environment in Ubuntu
How to install and use Docker on CentOS 7
How to use Nginx&#39;s map module on Ubuntu 16.04
How to install and use Docker on Ubuntu 20.04
How to use dpkg command in Ubuntu system
How to install and use Curl on Ubuntu 18.04
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to use Docker data volumes on Ubuntu 14.04
How to install and use Composer on CentOS 8
How to use code running assistant in python
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
How to use Jenkins to build automatically on Ubuntu
How to install and use PostgreSQL on Ubuntu 16.04
How to install and use Curl on CentOS 8
How to install and use Docker on Ubuntu 16.04
How to use LVM to manage storage devices on Ubuntu 18.04
How to create and use MongoDB backups on Ubuntu 14.04
How to upgrade to Ubuntu 20.04
How to install and use MySQL Workbench on Ubuntu 18.04
How to use Putty to log in to ubuntu installed in VirtualBox
How to install and use Cockpit on CentOS 8/RHEL 8
How to use Let&#39;s Encrypt to protect Nginx on CentOS 8
How to upgrade to Ubuntu 20.04
How to comment python code
How to write Pythonic code
How to learn python quickly
How to uninstall python plugin
How Python implements FTP function
How to understand python objects
Use virtualbox to deploy ubuntu
How to upgrade to Ubuntu 16.04 LTS
​Ubuntu Class|What is a key ring and how to use it?