Python基础(filter)

arr = [1,2,3,1,1,3,6,9]
def f1(x):
    if x > 2:
        return True
arr1 = list(filter(f1,arr))
print(arr1)#[3, 3, 6, 9]

arr3 = ['a','b','C','',None,'123']
def f2(x):#过滤删除一个序列中的空字符串和None
    return x and x.strip()
arr4 = list(filter(f2,arr3))
print(arr4)#['a', 'b', 'C', '123']

 

posted @ 2019-05-16 17:03  周大侠小课堂  阅读(177)  评论(0编辑  收藏  举报