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

写一个段落python代码推理list深浅

2015-10-10 08:24 701 查看
主要是针对嵌套列表问题。

列表套列表,究竟子列表那个更深。。。


这个问题想着就烦。假设嵌套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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: