Python 如何将元组列表转换为列表列表?

Python 如何将元组列表转换为列表列表?,python,Python,我是编程新手,我需要一些帮助。 我有一张这样的清单 a=[[('the', 'b'), ('hotel', 'i')],[('the', 'b'), ('staff', 'i')]] output=[['the', 'b', 'hotel', 'i'],['the', 'b', 'staff', 'i']] 我试图去掉元组,同时将数据保留在列表中,结果应该是这样的 a=[[('the', 'b'), ('hotel', 'i')],[('the', 'b'), ('staff', 'i')]

我是编程新手,我需要一些帮助。 我有一张这样的清单

a=[[('the', 'b'), ('hotel', 'i')],[('the', 'b'), ('staff', 'i')]]
output=[['the', 'b', 'hotel', 'i'],['the', 'b', 'staff', 'i']]
我试图去掉元组,同时将数据保留在列表中,结果应该是这样的

a=[[('the', 'b'), ('hotel', 'i')],[('the', 'b'), ('staff', 'i')]]
output=[['the', 'b', 'hotel', 'i'],['the', 'b', 'staff', 'i']]

非常感谢

您可以执行以下列表:

>>> [[y for x in i for y in x] for i in a]
[['the', 'b', 'hotel', 'i'], ['the', 'b', 'staff', 'i']]

请注意,这与元组无关,由于duck类型,元组的处理方式与列表理解中的列表完全相同。基本上,您正在对多个列表项执行中所述的操作。

这可以通过
sum
功能完成:

a=[[('the', 'b'), ('hotel', 'i')],[('the', 'b'), ('staff', 'i')]]
output = [sum(elem, ()) for elem in a]
print(output)
如果必须返回列表:

a=[[('the', 'b'), ('hotel', 'i')],[('the', 'b'), ('staff', 'i')]]
output = [sum(map(list,elem), []) for elem in a]
print(output)
我想你可以使用:

output = []
for x in a:
    output.append([element for tupl in x for element in tupl])

产出:

[['the', 'b', 'hotel', 'i'], ['the', 'b', 'staff', 'i']]
这里是@nfn neil's a的一个“功能”式变体

from itertools import repeat

list(map(list, map(sum, a, repeat(()))))
# -> [['the', 'b', 'hotel', 'i'], ['the', 'b', 'staff', 'i']]

搜索中缺少的关键字是“展平”,如“如何展平列表”。