filter函数

movie_people=['alex','sb_wwo','asw','ur_sb']
#print(list(filter(lambda n:n.endswith('sb'),movie_people)))
res=list(filter(lambda n:not n.endswith('sb'),movie_people))
print(res)

filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判断,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

以下是 filter() 方法的语法:

filter(function, iterable)

猜你喜欢

转载自www.cnblogs.com/banzui/p/9219775.html