python中的map()函数应用于每一个可迭代的项,返回的是一个结果list。map()接受两个参数,一个是函数,一个是序列。
实例:map(function,iterable,…)
举例学习:
【参考】python中的map()函数_o_w_olf的博客-CSDN博客
L=[1,2,3,4]
def pow2(x):
return x*x
list(map(pow2,L))运行结果:[1,4,9,16]
list(map(str,[1,2,3,4,5,6,7,8]))
运行结果:[‘1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’]
把数字转换为字符串
list(map(lambda x:x**2,[1,2,3,4,5]))#使用lambda匿名函数
运行结果:[1,4,9,16,25]
list(map(lambda x,y:x+y,[1,3,5,7,9],[2,4,6,8,10]))
运行结果:[3,7,11,15,19]