您的位置:首页 > 编程语言 > Python开发

python--多维list转化为1维度list,,递归

2017-08-12 20:05 295 查看
递归实现

#多维list转化为1维度list,,递归

##递归一定要学好

def getnewList(newlist):
d = []
for element in newlist:
if not isinstance(element,list):
d.append(element)
else:
d.extend(getnewList(element))

return d

list1 =['a',[1,['c','f'],2],'b'] # 结果 ['a', 1, 'c', 'f', 2, 'b']

print(getnewList(list1))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: