我有两个列表。
说,
letterList = [(1, 'a') (1, 'b')]
bigramList = [(2, 'ab'), (2, 'cd'), (2, 'ef')]
我想将两个列表映射在一起以获得此输出:
print(myMap)
# [ ((1, a), (2, ab)), ((1, b), (2, cd)), (None, (2, ef)) ]
我得到这样的输出:<map object at 0x7f639461efd0>
我知道我的问题出在这一行:
myMap = (map(None, letterList, biGramList))
我认为这是因为 Python 3。只是不太确定如何修复它并获得我想要的输出。
最佳答案
>>> letterList = [(1, 'a'), (1, 'b')]
>>> bigramList = [(2, 'ab'), (2, 'cd'), (2, 'ef')]
>>> list(itertools.zip_longest(letterList, bigramList))
[((1, 'a'), (2, 'ab')), ((1, 'b'), (2, 'cd')), (None, (2, 'ef'))]
请注意,您不一定需要将其强制到列表中才能使用它,它只是方便打印。
关于python - 如何在Python 3中映射两个不同长度的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063781/