Counting depth or the deepest level a nested list goes to

前端 未结 13 1771
無奈伤痛
無奈伤痛 2020-12-02 21:04

A have a real problem (and a headache) with an assignment...

I\'m in an introductory programming class, and I have to write a function that, given a list, will retur

13条回答
  •  忘掉有多难
    2020-12-02 21:25

    A short addition to what has been said so it can handle empty lists too:

    def list_depth(list_of_lists):
        if isinstance(list_of_lists, list):
            if(len(list_of_lists) == 0):
                depth = 1
            else:
                depth = 1 + max([list_depth(l) for l in list_of_lists])
        else:
            depth = 0
        return depth
    

提交回复
热议问题