-
筛选序列
-
filter(func, lst),将func作用于lst的每个元素,然后根据返回值是True或False判断是保留还是丢弃该元素。
-
例子:判断是否是素数
-
代码如下:
def prim(n):
for x in range(2,n):
if n%x==0:
#不是素数
return 1
#是素数
return 0
isNotPrimList=list(filter(prim,[3,4,5,6,7,8,9]))
print(isNotPrimList)
运行结果: