map()内置函数-创新互联

首先,我们来看下map()内置函数的用法:

创新互联公司主要从事做网站、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务博兴,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

>>> 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]

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:map()内置函数-创新互联
转载注明:http://ybzwz.com/article/pdogg.html