Dictionary manipulation
In this section we will illustrate some basic manipulations on dictionaries using the
following dictionary to do this:
d = {'Ala':71.07, 'Arg':156.18}
Dictionaries map keys to values. For the above dictionary we have two keys, ‘Ala’ and
‘Arg’, where the key ‘Ala’ gets mapped to the value 71.07 and the key ‘Arg’ gets mapped
to the value 156.18. The elements of a dictionary are not stored in any particular order,
thus it would be meaningless to access them by position. Instead they are accessed by key.
Although Python uses curly brackets to define dictionaries, when you want to access the
value of a key in a dictionary you use square brackets; myDict[key].
x = d['Ala'] # 71.07
If you try to get the value for a key that does not exist, then an error is generated (an
Exception object). There is an alternative syntax, however, which can be used to access a
value and that does not generate an error if an unknown key is used. This is the
dictionary’s get() function, which returns the corresponding value if the key is known and
None otherwise:
x = d.get('Ala') # 71.07
x = d.get('Gly') # None
There is an optional second argument to the get() function that specifies the default
value to use if the key is not in the dictionary:
x = d.get('Ala', 57.05) # 71.07
x = d.get('Gly', 57.05) # 57.05, because 'Gly' is absent from d
Note that if the key is in the dictionary then this default value is ignored. Also, the
dictionary is not changed by this call, so that after the second line above, the dictionary
still does not have a key for ‘Gly’. If you want this kind of side effect to happen, then
there is the slightly oddly named function setdefault() that you can use instead. This
behaves exactly like get() (so the fact that the name starts with ‘set’ looks misleading)
except that as well as returning a value it also adds the key to the dictionary with the
default value if the key does not exist (which is why ‘set’ is not totally misleading):
x = d.setdefault('Ala', 57.05) # 71.07
# d remains {'Ala': 71.07, 'Arg': 156.18}
x = d.setdefault('Gly', 57.05) # 57.05
# d becomes {'Ala':71.07, 'Arg':156.18, 'Gly':57.05}
You can check whether a key is in a dictionary and, unlike with tuples and lists,
checking containment in this way is an efficient computation:
'Ala' in d # True
'Thr' in d # False
An alternative, equivalent method for Python 2 is:
d.has_key('Ala') # True
d.has_key('Thr') # False
You can also check whether a key is not in a dictionary:
'Ala' not in d # False
'Thr' not in d # True
The len() function returns the number of (key, value) entries in the dictionary:
len(d) # 3
You can change the value for an existing key by reassigning it:
d['Ala'] = 71.04
# d now {'Ala':71.04, 'Arg':156.18, 'Gly':57.05}
You can introduce new entries in the dictionary by assigning a value with a new key:
d['Ser'] = 87.07
# d now {'Ala':71.04, 'Arg':156.18, 'Gly':57.05, 'Ser':87.07}
You can delete entries from the dictionary:
del d['Ala']
# d now {'Arg':156.18, 'Gly':57.05, 'Ser':87.07}
This will generate an error if the key is not in the dictionary. You can get hold of the
keys in the dictionary:
x = d.keys() # ['Gly', 'Arg', 'Ser']
The keys of a dictionary effectively form a set, but sets were introduced in Python long
after dictionaries, so for historic reasons in Python 2 the keys() function gives back a list,
rather than a set. Nonetheless it still makes no sense to talk about the order of the entries in
a dictionary, and thus also the order of the list that is returned. Although the list of keys is
nominally ordered, the order cannot be relied upon.
You can also get hold of the values associated with all of the keys from the dictionary:
x = d.values() # [57.04, 156.18, 87.07]
Unlike keys, the values in a dictionary can be repeated. So strictly speaking what we
should get is what mathematicians call a multiset, or a bag. But Python has no inbuilt data
type for this, and we are given a list, although again it cannot be relied upon to have a
specific order.
Finally, you can get hold of a list of (key, value) pairs as tuples:
x = d.items() # [('Gly', 57.04), ('Arg', 156.18), ('Ser', 87.07)]
In Python 3 the keys(), values() and items() functions change to return what is called a
view rather than a list, which is a subtle difference that means, for example, if you really
want to get hold of a set or a list then in Python 3 you would have to do so via
set(d.keys()) or list(d.values()) etc.
Do'stlaringiz bilan baham: |