现在的位置: 首页 > 综合 > 正文

函数编程-map()

2013年02月26日 ⁄ 综合 ⁄ 共 893字 ⁄ 字号 评论关闭

>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4]
>>> ll
[2, 4, 5, 6, 7, 8, 9, 4, 1, 5, 23, 45, 345, 56, 11, 3]
>>> help(map)
Help on built-in function map:

map(...)
    map(function, sequence[, sequence, ...]) -> list

    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).

>>> map(lambda x,y :x<y and x or 0,l,ll)
[1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 2, 3, 4, 0, 0, 0]
>>> map(None,l,ll)
[(1, 2), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8), (7, 9), (8, 4), (9, 1), (10, 5)
, (2, 23), (3, 45), (4, 345), (None, 56), (None, 11), (None, 3)]
>>> map(l,ll)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'list' object is not callable
>>> map(None,ll)
[2, 4, 5, 6, 7, 8, 9, 4, 1, 5, 23, 45, 345, 56, 11, 3]

抱歉!评论已关闭.