python3 的 filter 与 map

lists = [1, 2, 3, 4, 5, 6, 7]

print(list(filter(lambda x: x > 4, lists)))
# [5, 6, 7]

print(list(map(lambda x: x > 4, lists)))
# [False, False, False, False, True, True, True]


## 上述效果的另外一种写法
print([i for i in lists if i > 4])
# [5, 6, 7]

print([True if i > 4 else False for i in lists])
# [False, False, False, False, True, True, True]


## 扩展
print(lists[lists.index(4):])
# [4, 5, 6, 7]
# lists.index(4) 拿到值为4的列表下标,即3
# lists[lists.index(4):] == lists[3:] == [4, 5, 6, 7]

 

posted @ 2022-06-27 20:42  xuecl  阅读(48)  评论(0编辑  收藏  举报