python map的学习笔记

map

# map(function, iterable, ...) function -- 函数  iterable -- 一个或多个序列
 # Python 2.x
        # 返回列表。
    # Python3.x
        # 返回迭代器。
    #map会把iterable里的值依次放入function执行,返回迭代器

res = map(lambda x, y: (x ** y, x + y), [2, 4, 6], [3, 2, 1]) print(tuple(res)) # ((8, 5), (16, 6), (6, 7)) print(tuple(res)) # () 注意map内部使用了迭代器,再次使用就为空了 res = map(lambda x, y: (x ** y, x + y), [2, 4, 6], [3, 2, 1]) #map多数以lambda表达式配合使用,简单粗暴 _res = tuple(res) print(_res) # 将它赋值给一个变量来存储 a = [2, 3, 4, 5, 1, 1, 2] b = [3, 42, 3, 4, 32, 2] def func(x, y): if x or y: return x * y return 0 res = list(map(func, a, b)) # 多变量传参 print(res) # [6, 126, 12, 20, 32, 2]

 

posted @ 2020-09-23 10:48  RainBol  阅读(126)  评论(0编辑  收藏  举报
Live2D