在Python中合并两个嵌套列表 [英] Merge two nested lists in Python
本文介绍了在Python中合并两个嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表:
x = [['a', 1], ['b', 2], ['c', 3]]
y = [['a', 4], ['c', 6]]
我只想保留这些字母的常用元素,并将2个列表合并成:
I would like to keep only the common elements of the letters and combine the 2 lists into:
[['a', 1, 4], ['c', 3, 6]]
如何做到这一点?
推荐答案
>>> x = [['a', 1], ['b', 2], ['c', 3]]
>>> y = [['a', 4], ['c', 6]]
>>> lazy = dict
>>> lazyx = lazy(x)
>>> lazyy = lazy(y)
>>> [[lazy, lazyx[lazy], lazyy[lazy]] for lazy in lazyx if lazy in lazyy]
[['a', 1, 4], ['c', 3, 6]]
这篇关于在Python中合并两个嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文