2024-05-29 03:02:28 发布
网友
有什么技术可以知道嵌套JSON将包含的级别数吗?你知道吗
例如:
animals = [ { "animal" : { "type" : "bunny" } }, { "animal" : {} }, {} ]
您可以创建一个简单的递归函数:
def get_depth(d): c = [1 if not isinstance(b, dict) else 1+get_depth(b) for a, b in d.items()] return max(c) if c else 0 animals = [{'animal': {'type': 'bunny'}}, {'animal': {}}, {}] print(max(map(get_depth, animals)))
输出:
2
您可以创建一个简单的递归函数:
输出:
相关问题 更多 >
编程相关推荐