温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

map()内置函数

发布时间:2020-05-14 23:56:16 来源:网络 阅读:219 作者:麦迪门徒 栏目:编程语言

    首先,我们来看下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]

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI