Counting depth or the deepest level a nested list goes to

前端 未结 13 1731
無奈伤痛
無奈伤痛 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:41

    easy with recursion

    def flat(l):
        depths = []
        for item in l:
            if isinstance(item, list):
                depths.append(flat(item))
        if len(depths) > 0:
            return 1 + max(depths)
        return 1
    

提交回复
热议问题