1

I've found functions that define multi-dimensional dictionaries, but I'm unclear what they look like.

4

2 回答 2

3

The values in a dictionary can be any Python object, including another dictionary. For example:

animals = {'pets': {'George': 'cat', 'Fred': 'dog'}}

You can access one the values of the inner dictionary using a key lookup on animals like animals['pets']['George'] or animals['pets']['Fred'].

于 2013-06-11T19:36:14.563 回答
2

It seems more proper to call it a nested dictionary since the term multidimensional fits better to a regular shape, or rectangular shape, like (3x4x5x6).

A nested dictionary is a dictionary that contains another dictionary in one of its values, similarly to a nested list which is a list that contains another list in one of its values

Nested dictionary:

nested_dict = dict( a=1, b=2, c=dict(c1=2, c2=2), d=3, e=dict(e1=dict(e11=1, e12=2), e2=1))

{'a': 1,
 'b': 2,
 'c': {'c1': 2, 'c2': 2},
 'd': 3,
 'e': {'e1': {'e11': 1, 'e12': 2}, 'e2': 1}}

Analogy with a nested list:

nested_list = [1, 2, [2,2], 3, [[1,2],1]]

[1, 2, [2, 2], 3, [[1, 2], 1]]
于 2013-06-11T20:09:10.780 回答