python中多层嵌套列表的拆分方法
时间:2021-05-20 09:29:38|栏目:Python代码|点击: 次
场景:有一个多层嵌套的列表如:[[23],[3,3],[22,22],1,123,[[123,a],2]] 拆分成:
def splitlist(list):
'''
现有一个列表,里面元素包括 数字,字母,列表,字典等元素,现在要将字典去掉,并将列表
分解成字母,或数字元素如:[[1,2,3],2,3,[1,3,[12,22]],'a',12]
经函数处理后[1, 2, 3, 2, 3, 1, 3, 12, 22, 'a', 12]
'''
alist = []
a = 0
for sublist in list:
try: #用try来判断是列表中的元素是不是可迭代的,可以迭代的继续迭代
for i in sublist:
alist.append (i)
except TypeError: #不能迭代的就是直接取出放入alist
alist.append(sublist)
for i in alist:
if type(i) == type([]):#判断是否还有列表
a =+ 1
break
if a==1:
return printlist(alist) #还有列表,进行递归
if a==0:
return alist
list=[[1,2,3],2,3,[1,3,[12,22,[2,3]]],'a',12,range(10)]
a = printlist(list)
print(a)
打印结果:
[1, 2, 3, 2, 3, 1, 3, 12, 22, 2, 3, 'a', 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
分析:运用递归一层一层的去掉嵌套列表的数
栏 目:Python代码
下一篇:Python中跳台阶、变态跳台阶与矩形覆盖问题的解决方法
本文标题:python中多层嵌套列表的拆分方法
本文地址:http://www.codeinn.net/misctech/125237.html






