Counting depth or the deepest level a nested list goes to

前端 未结 13 1712
無奈伤痛
無奈伤痛 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
    
    0 讨论(0)
提交回复
热议问题