Python:合并嵌套列表 [英] Python: merge nested lists

查看:192
本文介绍了Python:合并嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里是python的初学者.

beginner to python here.

我有2个要合并的嵌套列表:

I have 2 nested lists that I want to merge:

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)

Can this be done without any external libraries? note: len(list1) = len(list2)

推荐答案

使用 zip 函数和列表理解:

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:合并嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆