Python map() 函数

http://www.runoob.com/python/python-func-map.html

map() 函数语法:

map(function, iterable, ...)

参数

  • function -- 函数,有两个参数
  • iterable -- 一个或多个序列
  • >>>def square(x) :            # 计算平方数
    ...     return x ** 2
    ... 
    >>> map(square, [1,2,3,4,5])   # 计算列表和: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]
原文地址:https://www.cnblogs.com/tester-go/p/7808695.html