我有
g = [[1], [2], [3], [4], [5], [6]] c = [[9], [8], [7], [6], [5], [4]]
我需要得到
t = [[1,9], [2,8], [3,7], [4,6], [5,5], [6,4]]
请帮帮我
使用此处的zip()
函数组合嵌套列表的元素:
t = [a + b for a, b in zip(g, c)]
演示:
>>> g = [[1], [2], [3], [4], [5], [6]] >>> c = [[9], [8], [7], [6], [5], [4]] >>> [a + b for a, b in zip(g, c)] [[1, 9], [2, 8], [3, 7], [4, 6], [5, 5], [6, 4]]
使用Python 3 /中的迭代zip()
函数itertools
可以略微提高效率:
try: from future_builtins import zip except ImportError: pass t = [a + b for a, b in zip(g, c)]
的try..except
,如果你想要的代码,以在两个Python 2和3的工作才是必需的.