写段python代码判断list深度


主要是针对嵌套列表问题。列表套列表,到底子列表那个更深。。。

这个问题想着就烦。如果嵌套10000万个列表是不是要统计10000个数再排序呢?


最后想了想用 list的extend功能 加上递归函数尝试了一下,代码如下:


l1=[1,'a',[1],[2,3,[4,5,[6,7,[7]]]],[2,5,[5,6]],[4],[5],[6]]

#l1 = [1, 2, [3, [4, 5], 6, [7, 8,[9, 10], 11], 12], 13]
count = 1

def func(l):
    global count
    
    temp_list = []
    for l2 in l:
        if isinstance(l2,list)==True:
            temp_list.extend(l2)
    if len(temp_list)>0:
        count +=1
        func(temp_list)
        
            

func(l1)
print count


评论关闭