马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> list=[1, [2], [[3], 4, [[[5, 6], 7], 8]], 9]
def deep_flatten(lst):
result = []
for i in lst:
if isinstance(i, list):
result.extend(deep_flatten(i))
else:
result.append(i)
return result
deep_flatten(list)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
deep_flatten(list)
File "<pyshell#4>", line 4, in deep_flatten
if isinstance(i, list):
TypeError: isinstance() arg 2 must be a type or tuple of types
求大佬帮忙看下,谢谢
list 是Python 的内置函数 你把他赋值了还怎么用 isinstance 判断类型呢?
list1=[1, [2], [[3], 4, [[[5, 6], 7], 8]], 9]
def deep_flatten(lst):
result = []
for i in lst:
if isinstance(i, list):
result.extend(deep_flatten(i))
else:
result.append(i)
return result
deep_flatten(list1)
|