Why map(print, a_list) doesn't work?

后端 未结 6 1897
野趣味
野趣味 2020-12-18 19:55

For a normal function, map works well:

def increment(n):
    return n+1
l = [1, 2, 3, 4, 5]
l = map(increment, l)
print l
>>> [2, 3, 4,         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 19:57

    Because print is not a function.

    But you can make print-wrapper, of course:

    >>> def p(x):
    ...   print x
    ... 
    >>> l = [1, 2, 3, 4, 5]
    >>> l = map(p, l)
    1
    2
    3
    4
    5
    

提交回复
热议问题