Python-Ref > Data structures > Dictionaries > Storing more complex values in a dictionary
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Storing more complex values in a dictionary

While the type of keys that may be used in a dictionary are limited to immutable types, the values may have any type conceivable. Thus the dictionary values may be lists, objects, functions or even other dictionaries.
The following examples show some possible structures based on dictionaries.
Expand/Shrink
Zdroj: (dict3-1.py)
  1   # dictionary of dictionaries
  2   # employees = id->employee record
  3   employees = { 102: {"first_name": "Jane",
  4                       "family_name": "Smith",
  5                       "department": 101},
  6                 114: {"first_name": "Arthur",
  7                       "family_name": "Dent",
  8                       "department": 42},
  9                 253: {"first_name": "Ford",
 10                       "family_name": "Prefect",
 11                       "department": 999}
 12                 }
 13   
 14   print employees[114]
 15   print employees[253]["family_name"]
stdout:
{'department': 42, 'family_name': 'Dent', 'first_name': 'Arthur'}
Prefect
Doba běhu: 21.1 ms
Expand/Shrink
Zdroj: (dict3-2.py)
  1   # dictionary assigning measurements to each of miss contestants
  2   miss2meas = { "Jane": (86, 62, 92),
  3                 "Anne": (90, 60, 90),
  4                 "Lucy": (92, 59, 90),
  5                 "Mary": (86, 57, 87) 
  6                 }
  7   
  8   
  9   print miss2meas["Lucy"]
 10   print miss2meas["Lucy"][0]
 11   
 12   for miss, measurement in miss2meas.iteritems():
 13       print miss, measurement[1]
stdout:
(92, 59, 90)
92
Jane 62
Anne 60
Lucy 59
Mary 57
Doba běhu: 21.2 ms