简体   繁体   English

将嵌套元组转换为 python 中的嵌套列表

[英]Converting nested tuple into nested list in python

I have nested tuple like below wanted to convert into nested list in the format mentioned我有像下面这样的嵌套元组,想以提到的格式转换成嵌套列表

INPUT T = [('id','1'),('name','Mike'),('Adrs,'Tor')]

OUTPUT L = [['id','1'],['name','Mike'],['Adrs,'Tor']]

I tried to do following我试着做以下

L = []

for item in T:
    L.append(item)

L still gives me same. L仍然给我同样的。

>>> spam = [('id','1'),('name','Mike'),('Adrs','Tor')]
>>> eggs = [list(item) for item in spam]
>>> eggs
[['id', '1'], ['name', 'Mike'], ['Adrs', 'Tor']]

You could unpack each separate tuple and then append it您可以解压缩每个单独的元组,然后解压缩 append

L = []

for item in T:
    a, b = item
    L.append([a,b])
l = [('id','1'),('name','Mike')]
m = [[], []]
for i in range(len(l)):
    m[i] = [l[i][0], l[i][1]]
print(m)

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

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