python 中 filter的用法

filter( , )

该函数有两个参数,第一个参数是一个函数,第二个是一个序列,

函数的返回值是使得第一个参数中的函数为true的序列中的元素

比如

def is_odd(n):
    return n % 2 == 1


new_list = list(filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8]))
print(new_list)
  

猜你喜欢

转载自blog.csdn.net/qfpkzheng/article/details/80024817