将嵌套list(嵌套层数不定,多层嵌套)
list = [3,4,6,[8,5,3],2,4,6,8,[9,65,[33,3],2,22,1],1,33,[56,[5,7],6,88],32,2]
如何转换为简单list
[3,4,6,8,5,3,2,4,6,8,9,65,33,3,2,22,1,1,33,56,5,7,6,88,32,2]
试试这个,可以的话点个赞吧,加油,同学
a = str(list).replace(']','').replace('[','')
l = [int(x) for x in a.split(',')]
print(l)
regular_list = [3, 4, 6, [8, 5, 3], 2, 4, 6, 8, [9, 65, [33, 3], 2, 22, 1], 1, 33, [56, [5, 7], 6, 88], 32, 2]
def flatten(list_of_lists):
if len(list_of_lists) == 0:
return list_of_lists
if isinstance(list_of_lists[0], list):
return flatten(list_of_lists[0]) + flatten(list_of_lists[1:])
return list_of_lists[:1] + flatten(list_of_lists[1:])
print(flatten(regular_list))