filter()
In [130]: a=[i for i in range(10)]
In [131]: def aaa(b):
...: return b>0
...:
In [132]: list(filter(aaa,a))
Out[132]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
本文介绍如何利用Python内置函数filter()对列表进行条件筛选。通过定义一个判断函数aaa(b),该函数返回True如果参数b大于0。接着使用filter()结合aaa()函数过滤一个由0到9的数字列表,得到所有正数。
filter()
In [130]: a=[i for i in range(10)]
In [131]: def aaa(b):
...: return b>0
...:
In [132]: list(filter(aaa,a))
Out[132]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
301
279

被折叠的 条评论
为什么被折叠?