python函数map

发布时间:2019-07-14 10:00:28编辑:auto阅读(1219)

    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的参数执行,将结果作为一个列表返回。

    >>> 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(并行)

    >>> def add(a,b):
    ... return a+b
    ... 
    >>> a=[1,2,3]
    >>> b=[4,5,6]
    >>> map(add,a,b)
    [5, 7, 9]
    >>>

    翻译不了了:

    就这样吧。

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


关键字

上一篇: Python列表之元组

下一篇: python 爬取mm信息