Python programming language provides filter()
function in order to filter a given array, list, dictionary, or similar iterable struct. filter()
function can be used to create iterable by filtering some elements of the given data.
Python编程语言提供了filter()
函数,以便过滤给定的数组,列表,字典或类似的可迭代结构。 filter()
函数可用于通过过滤给定数据的某些元素来创建可迭代的对象。
Python筛选器功能语法 (Python Filter Function Syntax)
filter()
function has the following syntax. FUNCTION is the function name we will use to test the given dataset and create a new iterable list. ITERABLE is the data we will check with our function and filter.
filter()
函数具有以下语法。 FUNCTION是用于测试给定数据集并创建新的可迭代列表的函数名称。 ITERABLE是我们将通过函数和过滤器检查的数据。
filter(FUNCTION, ITERABLE)
筛选清单 (Filter List)
The most popular usage of the filter()
function is using Python List. We will provide a list which will be filtered with the given function. In this example, we will provide the list named numbers
and use oddF'lter()
function in order to filter numbers
.
filter()
函数最流行的用法是使用Python列表。 我们将提供一个列表,该列表将使用给定的函数进行过滤。 在此示例中,我们将提供名为numbers
的列表,并使用oddF'lter()
函数来过滤numbers
。
#!/bin/python3
numbers=[1,2,3,4,5,6,7,8,9]
def oddFilter(number):
if(number%2==0):
return True
else:
return False
odd_numbers=filter(oddFilter,numbers)
for number in odd_numbers:
print(number)
We will provide the list named numbers
. This list contains numbers from 1 to 9. We will create a filter function named oddFilter()
which will filter for odd numbers in the given list and return True if the given element is odd or return False if the given element is even. Then we will add odd numbers to a new list named odd_numbers
.
我们将提供名为numbers
的列表。 该列表包含从1到9的数字。我们将创建一个名为oddFilter()
的过滤器函数,该函数将过滤给定列表中的奇数,如果给定元素为奇数,则返回True;如果给定元素为偶数,则返回False。 然后,我们将奇数添加到名为odd_numbers
的新列表中。
筛选字典 (Filter Dictionary)
Dictionaries can be also filtered with the filter()
function. We can filter dictionaries according to their key or value for each element. In this example, we will filter the dictionary named names
.
字典也可以使用filter()
函数进行filter()
。 我们可以根据字典中每个元素的键或值来过滤字典。 在此示例中,我们将过滤名为names
的字典。
#!/bin/python3
names={1:"ismail",2:"ali",3:"ahmet",4:"elif",5:"ecrin"}
filtered_dict = dict(filter(lambda item: item[0]%2==0 , names.items()))
for item in filtered_dict.items():
print(item)
过滤字符串列表(Filter String List)
We can also use filter()
function in order to filter the given string list. In this example, we will look for strings which contains i
letter.
我们还可以使用filter()
函数来过滤给定的字符串列表。 在此示例中,我们将查找包含i
字母的字符串。
#!/bin/python3
names=['ismail','ali','ahmet','elif','ecrin']
def filterString(name):
if 'i' in name:
return True
else:
return False
filtered_names=filter(filterString,names)
for name in filtered_names:
print(name)
使用Lambda进行过滤(Filter Using Lambda)
Lambda is very useful when we want not to use a function. As filter()
function requires a function we can skip defining a new function and use lambda like a function. We will filter those strings which contain the letter i
.
当我们不想使用函数时,Lambda非常有用。 由于filter()
函数需要一个函数,因此我们可以跳过定义新函数的过程,而可以像函数一样使用lambda。 我们将过滤那些包含字母i
字符串。
#!/bin/python3
names=["ismail","ali","ahmet","elif","ecrin"]
filtered_names = filter(lambda item: 'i' in item , names)
for item in filtered_names:
print(item)