Python-Ref > Data structures > Dictionaries > Dictionary introduction
 
 

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

Dictionary introduction

Dictionaries, called associative arrays or hashes in other programming languages, are used to store data in key-value pairs. In usage, they resemble arrays, with the difference that their indexes (keys) are not intergers between 0 and n but arbitrary non-mutable objects, usually strings, integers, etc.
Expand/Shrink
Zdroj: (dict1-1.py)
  1   # creation of a dictionary
  2   name2age = {"Jane": 21, "Lucy": 34, "David": 1, "Tom": 5, "Peter": 6}
  3   
  4   print name2age['Jane']
  5   print name2age['David']
  6   print "len =", len( name2age)
  7   name2age['Mike'] = 90
  8   print "len =", len( name2age)
  9   print name2age['Mike']
stdout:
21
1
len = 5
len = 6
90
Doba běhu: 21.6 ms
Dictionaries are usually used in two different ways. One is to associate some values with an unique identifier - personal records with name, product information with product code, etc.
The following piece of code shows how to count abundance of letters in a string.
Expand/Shrink
Zdroj: (dict1-2.py)
  1   text = "Marry had a little lamb."
  2   char2count = {}
  3   
  4   for ch in text:
  5       if ch in char2count:
  6           char2count[ch] += 1
  7       else:
  8           char2count[ch] = 1
  9   
 10   print char2count.keys()   # list of key values
 11   print char2count.values()   # list of values (in same order as keys)
 12   
 13   for ch in char2count.keys():
 14       print ch, char2count[ch]
 15       
stdout:
['a', ' ', 'b', 'e', 'd', 'i', 'h', 'M', 'l', '.', 'm', 'r', 't', 'y']
[4, 4, 1, 1, 1, 1, 1, 1, 3, 1, 1, 2, 2, 1]
a 4
  4
b 1
e 1
d 1
i 1
h 1
M 1
l 3
. 1
m 1
r 2
t 2
y 1
Doba běhu: 21.5 ms
Please note that the keys are not reported in the order in which they were added into the dictionary. The order is unspecified, and could in fact change with each new start of the program.
The other way to use dictionaries is to create a complex data structure resembling a database record. Such records may the be stored in a list to get something similar to a database.
Expand/Shrink
Zdroj: (dict1-3.py)
  1   students = [{"first_name":    "Peter",
  2                "family_name":   "Steward",
  3                "study_program": "informatics",
  4                "class":         "A106"},
  5               {"first_name":    "Douglas",
  6                "family_name":   "Adams",
  7                "study_program": "writing",
  8                "class":         "B025"},
  9               {"first_name":    "Miloslav",
 10                "family_name":   "Nic",
 11                "study_program": "ecology",
 12                "class":         "D003"},
 13               ]
Doba běhu: 20.5 ms
Even though it is possible to use lists for this purpose, using dictionaries is more natural. In case of lists one would have to remember on which position which kind of information is located.
The fact that dictionaries establish a pairwise key-value relationship is reflected in some of its methods as well as in its constructor.
Expand/Shrink
Zdroj: (dict1-4.py)
  1   # creation of a dictionary
  2   name2age = {"Jane": 21, "Lucy": 34, "David": 1, "Tom": 5, "Peter": 6}
  3   
  4   print name2age.items()  # the key-value pairs
  5   
  6   # dictionary creation from key-value pairs
  7   name2kids = dict( [("Jane", 1), ("Lucy", 3), ("David",0), ("Tom", 0), ("Peter", 0)])
  8   print name2kids
stdout:
[('Jane', 21), ('Tom', 5), ('Lucy', 34), ('Peter', 6), ('David', 1)]
{'Jane': 1, 'Tom': 0, 'Lucy': 3, 'Peter': 0, 'David': 0}
Doba běhu: 21.3 ms