map()函数的语法如下:
map(function,iterable,...)
map()函数是根据提供的函数(即function)对指定的序列(即iterable)做映射。
简单说就是iterable指向的每一个元素都会调用function函数,返回每次function函数返回值的新列表。
-- 参数 function是调用的函数
-- 参数 iterable是一个或多个序列
返回值:
python2返回列表
python3返回迭代器
使用示例1:
- # python2.x
-
- def square(x):
- return x**x
-
- l = map(square,[1,2,3,4,5,6])
- print(l)
- print(type(l))
显示结果:
使用示例2:
- # python3.x
-
- def square(x):
- return x**x
-
- l = list(map(square,[1,2,3,4,5,6])) # 使用list转换为列表
- print(l)
- print(type(l))
显示结果:
应用
1. 比如需要输入多个类型相同的数据时,就可以使用map
- x,y,z=list(map(int,input().split()))
- print(x,y,z,sep="\n")
全文参考:
Python map() 函数 | 菜鸟教程 (runoob.com)https://www.runoob.com/python/python-func-map.html