【发布时间】:2021-05-06 07:19:12
【问题描述】:
我正在寻找一个包含多个嵌套列表的列表:
list1 = [[1,2],[3,[4,5]],6,[7]] 输出 --> [1,2,3,4,5,6,7]
我刚刚加入 Stack Overflow,因此无法发布我对类似热门问题的回答。
因此,我在这里发布我认为最快的答案,如果有人得到更好的解决方案,请告诉我。同时,人们可以参考这个来处理:
a = str(list1) #Convert list to String
a = a.replace('[','')
a = a.replace(']','') # remove '['&']' from the list use specific brackets for other data structures
# in case of dictionary replace ':' with ','
b=a.split(',') # Split the string at ','
d = [int(x) for x in b]
这应该能够为任何级别的复杂列表做到这一点..
Dnyanraj Nimbalkar aka Samrat
【问题讨论】:
-
请通过intro tour、help center 和how to ask a good question 了解本网站的运作方式并帮助您改进当前和未来的问题,从而帮助您获得更好的答案。我们希望您在此处发布问题之前进行适当的研究。使用问题的标题很容易找到解决方案。
标签: python list dictionary tuples flatten