描述:
map()函数用于处理序列中的每个元素,得到结果是一个‘列表’(其实是个可迭代对象),该’列表‘元素个数及位置与原来一样。
语法:
map(func, *iterables)
func:处理逻辑,可以使用lambda,也可以使用其他函数
*iterables:传入的序列
实例1:
# 将列表中的每个数自加1
num_l = [1, 2, 10, 5, 3, 7]
res = map(lambda x: x + 1, num_l)
print('内置函数map,处理结果:', res)
print(list(res))
输出结果:
内置函数map,处理结果: <map object at 0x00000187D092B2E8> # 说明map()函数返回结果是一个可迭代对象
[2, 3, 11, 6, 4, 8]
实例2:
# 将字母全部转换成大写
msg = 'welcom'
print(list(map(lambda x: x.upper(), msg)))
输出结果:
['W', 'E', 'L', 'C', 'O', 'M']