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]

 

posted on   mlllily  阅读(56)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示