前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python函数map

python函数map

作者头像
py3study
发布2020-01-13 20:53:01
6030
发布2020-01-13 20:53:01
举报
文章被收录于专栏:python3python3

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended withNoneitems. If function isNone, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

Apply function to every item of iterable and return a list of the results.

将可迭代的iterable参数中的每一个元素作为function的参数执行,将结果作为一个列表返回。

代码语言:javascript
复制
>>> def foo(a):
...    return a**a
... 
>>> a
[1, 2, 3]
>>> map(foo,a)
[1, 4, 27]
>>>

 If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.

如果有多个可迭代的参数,所有的iterables会同时传递相同位置的元素给function(并行)

代码语言:javascript
复制
>>> def add(a,b):
... return a+b
... 
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> map(add,a,b)
[5, 7, 9]
>>>

翻译不了了:

就这样吧。

代码语言:javascript
复制
>>> c
[7, 8]
>>> a
[1, 2, 3]
>>> map(add,a,c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in add
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
>>> map(mm,a,c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mm' is not defined
>>> map(None,a,c)
[(1, 7), (2, 8), (3, None)]
>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> map(None,a,b)
[(1, 4), (2, 5), (3, 6)]
>>> c
[7, 8]
>>> map(None,a,c)
[(1, 7), (2, 8), (3, None)]
>>>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档