擅长:python、mysql、java
<p>您可以创建任何类型的多维字典,如下所示:</p>
<pre><code>from collections import defaultdict
from collections import Counter
def multi_dimensions(n, type):
""" Creates an n-dimension dictionary where the n-th dimension is of type 'type'
"""
if n<=1:
return type()
return defaultdict(lambda:multi_dimensions(n-1, type))
>>> m = multi_dimensions(5, Counter)
>>> m['d1']['d2']['d3']['d4']
Counter()
</code></pre>