不定期整理,仅用于学习和记录,如果有错误,请及时帮忙订正,谢谢~
1.Filter函数
1.1 使用方式
##语法
filter(function, iterable)
##Python 2.7返回的是一个list,Python 3返回的是一个迭代器
##举例
def greater_than_five(n):
return n > 5
newlist = filter(greater_than_five, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(newlist)
##Output: [6, 7, 8, 9, 10]
1.2 注意事项
##function设定为None
files = ["test.txt", "test.csv"]
newfiles = filter(None, files)
print newfiles
##Output: ["test.txt", "test.csv"]
"""
当function设定为None时,会过滤iterable中为False的值,如空字符串、False、[]、None、{}、()
依据:
----function - function that tests if elements of an iterable returns true or false
If None, the function defaults to Identity function - which returns false if any elements are false
---iterable - iterable which is to be filtered, co