在Python中,Itertools是內置模塊,可讓我們以高效的方式處理迭代器。它們非常容易地遍曆列表和字符串之類的可迭代對象。 filterfalse()是這樣的itertools函數之一。
注意:有關更多信息,請參閱Python Itertools。
filterfalse()函數
此迭代器僅輸出對於傳遞的函數返回false的值。
用法:
filterfalse(function or None, sequence) --> filterfalse object
參數:此方法包含兩個參數,第一個參數是function或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]
相關用法
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Python – Itertools.filterfalse()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
