Python – Itertools.filterfalse()
在Python中,Itertools 是内置模块,它允许我们以有效的方式处理迭代器。它们使迭代列表和字符串等可迭代对象变得非常容易。一个这样的 itertools函数是filterfalse()。
注意:更多信息请参考Python Itertools
filterfalse()函数
此迭代器仅打印为传递的函数返回 false 的值。
句法:
filterfalse(function or None, sequence) --> filterfalse object
参数:此方法包含两个参数,第一个参数是函数或 None,第二个参数是整数列表。
返回值:此方法返回为传递的函数返回 false 的唯一值。
示例 1:
# Python program to demonstrate
# the working of filterfalse
import itertools
from itertools import filterfalse
# function is a None
for i in filterfalse(None, range(20)):
print(i)
li = [2, 4, 5, 7, 8, 10, 20]
# Slicing the list
print(list(itertools.filterfalse(None, li)))
输出:
0
[]
示例 2:
# Python program to demonstrate
# the working of filterfalse
import itertools
from itertools import filterfalse
def filterfalse(y):
return (y > 5)
li = [2, 4, 5, 7, 8, 10, 20]
# Slicing the list
print(list(itertools.filterfalse(filterfalse, li)))
输出:
[2, 4, 5]
示例 3:
# Python program to demonstrate
# the working of filterfalse
import itertools
from itertools import filterfalse
li = [2, 4, 5, 7, 8, 10, 20]
# Slicing the list
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))
输出:
[5, 7]