美文网首页
map()函数 Python

map()函数 Python

作者: RayRaymond | 来源:发表于2020-04-30 14:21 被阅读0次
    map(function, iterable, ...)
    
    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.
    

    据提供的函数对指定序列做映射。
    把可迭代数据集合中每个元素调用function,返回值形成新的集合。

    >>>def square(x) :            # 计算平方数
    ...     return x ** 2
    ... 
    >>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
    [1, 4, 9, 16, 25]
    >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
    [1, 4, 9, 16, 25]
     
    # 提供了两个列表,对相同位置的列表数据进行相加
    >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
    [3, 7, 11, 15, 19]
    
    
    >>> list(map(lambda x : x / 2,range(1,6)))
    [0.5, 1.0, 1.5, 2.0, 2.5]
    

    Reference

    [1] https://www.runoob.com/python/python-func-map.html

    相关文章

      网友评论

          本文标题:map()函数 Python

          本文链接:https://www.haomeiwen.com/subject/zawawhtx.html