初学者在这里。
我有 2 个要合并的嵌套列表:
list1 = ['a', (b, c), (d, e), (f, g, h) ] list2 = [(p,q), (r, s), (t), (u, v, w) ]
我正在寻找的输出是:
list3 = [(a, p, q), (b, c, r, s), (d, e, t), (f, g, h, u, v, w) ]
这可以在没有任何外部库的情况下完成吗? 注意: len(列表1) = len(列表2)
使用的力量zip http://docs.python.org/library/functions.html#zip功能和列表推导式 http://docs.python.org/tutorial/datastructures.html#list-comprehensions:
list1 = [('a', ), ('b', 'c'), ('d', 'e'), ('f', 'g', 'h') ] list2 = [('p', 'q'), ('r', 's'), ('t', ), ('u', 'v', 'w') ] print [a + b for a, b in zip(list1, list2)]