首先,我们来看下map()内置函数的用法:
>>> help(map)
Help on class map in module builtins:class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __next__(self, /)
| Implement next(self).
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
map()内置函数有两个参数,第一个参数为函数,第二个参数为可迭代对象,将可迭代对象中的每个元素作为函数的
参数进行运算,可以看到,第二个参数亦是一个收集参数,所以当第二个元素为收集参数时,其支持多个可迭代对象,
可迭代对象会依次取一个元素组成一个元组,然后传递给函数进行运算,当可迭代对象长度不一致时,到最短的那个
终止
例:
>>> list(map(lambda x:x**2,range(10)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> list(map(lambda x,y:x+y,[1,8,10],[8,6,3,7,5,4]))
[9, 14, 13]
map()内置函数
原创
©著作权归作者所有:来自51CTO博客作者麦迪门徒的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:filter()内置函数
下一篇:字典
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python内置函数--map()映射
必知必会
python 迭代 迭代器 for循环 -
Python内置函数filter和map
Python内置函数filter和map
python 开发语言 迭代 空白符