python中的示例filter()
Python中的filter函数用于使用某些条件从可迭代对象中获取一些选定的元素。在本文中,我们将列出一个列表,并通过应用某些条件从中选择一些元素。
语法
filter(function, iterable) function: A Function to be run for each item in the iterable iterable: The iterable to be filtered
在下面的示例中,我们定义了一个函数,该函数会将数字除以2以检查是否有任何提示,然后确定数字是奇数还是偶数。使用将该功能应用于列表filter()。
示例
listA = [15, 8, 21, 13, 32]
def findeven(x):
if x %2 !=0:
return False
else:
return True
evenum = filter(findeven, listA)
for x in evenum:
print(x)输出结果
运行上面的代码给我们以下结果-
8 32
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短