擅长:python、mysql、java
<p>使用内置的<a href="https://docs.python.org/2/library/functions.html#len" rel="nofollow">len</a>、<a href="https://docs.python.org/2/library/functions.html#max" rel="nofollow">max</a>和<a href="https://wiki.python.org/moin/Generators" rel="nofollow">generator</a>试试这个:</p>
<pre><code>top = {
"somekey":"somevalue",
"sub1":
{"subsub1":1,"subsub2":2},
"sub2":
{"subsub1":1,"subsub2":2,"subsub3":3}
}
print max(len(item) for key,item in top.items() if isinstance(item,dict))
#3
</code></pre>
<p>您应该检查项目的类型,否则顶级键也会返回值,但实际上不是子部分。你知道吗</p>