简体   繁体   English

元组列表到单个列表

[英]list of tuples to single list

I have list of tuples in the following format,我有以下格式的元组列表,

[('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]

I need to convert them into following format,我需要将它们转换成以下格式,

[['ABC','32064','Woo'],
['ABC','32064','MISSI'],
['XYZ','32065','HAY']]

I have tried the following code我试过下面的代码

list1=[[('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]]
list2 = [item for sublist in list1 for item in sublist]
list2

but still producing the same result.但仍然产生相同的结果。

You could do it with a list comprehension:你可以用列表理解来做到这一点:

data = [('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]

[[t[0],t[1][0],x] for t in data for x in t[1][1]]

Output: Output:

[['ABC', '32064', 'WOO'], ['ABC', '32064', 'MISSI'], ['XYZ', '32065', 'HAY']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM