Python3中map函数
在Python2中map函数会返回一个list列表,如代码:
-
>>> def f(x, y): return (x, y)
-
>>> l1 = [ 0, 1, 2, 3, 4, 5, 6 ]
-
>>> l2 = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]
返回结果如下:
-
>>> map(f, l1, l2)
-
[(0, 'Sun'), (1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat')]
但是,在Python3中返回结果如下:
-
>>> map(f1, l1, l2)
-
<map object at 0x00000000021DA860>
如果想要得到Python2的那种结果,即返回list列表,那么必须用list作用于map,如下:
-
>>> list(map(f1, l1, l2))
-
[(0, 'Sun'), (1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat')]