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 fo
r 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()
:
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.
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.
filter()
for complex scenesFor 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.
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