map 函数
map() 函数的作用是:对序列 iterable 中每一个元素调用 function 函数,返回一个map对象实例。这个map对象本质上来讲是一个迭代器。
map函数基本语法:map(function, iterable)
students = [1, 2, 3, 4, 5] # 把列表中每个元素由int变为str: print(list(map(str, students))). # 输出['1', '2', '3', '4', '5'] # 把列表中每个元素*2 def double_func(x): return x* 2 print(list(map(double_func, students))) # 输出[2, 4, 6, 8, 10] # map+lambda表达式,将列表中元素乘2 print(list(map(lambda x: x*2, students))) # 输出[2, 4, 6, 8, 10]
map() 函数输入多个可迭代对象
b1 = [100, 200, 300] b2 = [1, 2, 3] iterator = map(lambda x,y : x*y, b1, b2) print(list(iterator)) # 输出:[100, 400, 900]
猪猪侠要努力呀!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?