编程语言
首页 > 编程语言> > Python:合并嵌套列表

Python:合并嵌套列表

作者:互联网

这里初学者到python.

我有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(list1)= len(list2)

解决方法:

使用zip功能和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)]

标签:python,merge,nested-lists
来源: https://codeday.me/bug/20190713/1451284.html