开一篇文章,记录关于Python有意思的用法,不断更新
1.Python树的遍历
1 def sum(t): 2 tmp=0 3 for k in t: 4 if not isinstance(k,list): 5 tmp+=k 6 else: 7 tmp+=sum(k) 8 return tmp 9 if __name__==‘__main__‘: 10 x=[1,[2,[3,4,5,[6,7,[8,9]]]]] 11 t=sum(x) 12 print(t)
时间: 2024-10-14 01:03:11