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]
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步