计算列表嵌套层数

2018-07-30  本文已影响0人  十二右
 23 def list_depth(items: list) -> int:
 24     max_depth = 1 if isinstance(items, list) else 0
 25     if max_depth:
 26         for item in items:
 27             if isinstance(item, list):
 28                 max_depth = max(max_depth, list_depth(item) + 1)
 29     else:
 30         return max_depth
 31     return max_depth
上一篇 下一篇

猜你喜欢

热点阅读