Calling none in maps in Python 3 [duplicate]

爱⌒轻易说出口 提交于 2020-01-11 05:33:48

问题


I am doing the following in Python2.7:

>>> a = [1,2,3,4,5]
>>> b = [2,1,3,4]
>>> c = [3,4]
>>> map(None, a, b, c)
[(1, 2, 3), (2, 1, 4), (3, 3, None), (4, 4, None), (5, None, None)]

I am trying to do something similar in Python3

>>> a = [1,2,3,4,5]
>>> b = [2,1,3,4]
>>> c = [3,4]
>>> map(None, a, b, c)
<map object at 0xb72289ec>
>>> for i,j,k in map(None, a, b, c):
...  print (i,j,k)
... 
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

How do I replicate the Python2 results in Python3?


回答1:


Use the itertools.zip_longest() function instead:

from itertools import zip_longest

for i, j, k in zip_longest(a, b, c):

This zips together the three lists, padding out with the fillvalue keyword value (defaulting to None).



来源:https://stackoverflow.com/questions/35002646/calling-none-in-maps-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!