I've found functions that define multi-dimensional dictionaries, but I'm unclear what they look like.
2 回答
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']
.
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]]