Python 3home |
Introduction to Python
davidbpython.com
A "counting" or "summing" dictionary answers the question "how many of each" or "how much of each".
Aggregations may answer the following questions:
The dict is used to store this information. Each unique key in the dict will be associated with a count or a sum, depending on how many we found in the data source or the sum of values associated with each key in the data source.
A "counting" dict increments the value associated with each key, and adds keys as new ones are found.
state_count = {} # empty dict
fh = open('revenue.csv')
for line in fh: # str, "Haddad's,PA,239.50\n"
items = line.split(',') # list, ["Haddad's", 'PA', '239.50\n']
state = items[1] # str, 'PA'
if state not in state_count:
state_count[state] = 0
state_count[state] = state_count[state] + 1
print(state_count) # {'PA': 2, 'NJ': 2, 'NY': 3}
fh.close()
Ex. 6.16
A "summing" dict sums the value associated with each key, and adds keys as new ones are found.
state_sum = {} # empty dict
fh = open('revenue.csv') # 'file' object
for line in fh: # str, "Haddad's,PA,239.50\n"
items = line.split(',') # ["Haddad's", 'PA', '239.50']
state = items[1] # str, 'PA'
value = float(items[2]) # float, 239.5
if state not in state_sum:
state_sum[state] = 0
state_sum[state] = state_sum[state] + value
print(state_sum) # {'PA': 263.45, 'NJ': 265.4, 'NY': 133.16}
fh.close()
len() counts the pairs in a dict.
mydict = {'a': 1, 'b': 2, 'c': 3}
print(len(mydict)) # 3 (number of keys in dict)
Ex. 6.21
This method may be used to retrieve a value without checking the dict to see if the key exists.
mydict = {'a': 1, 'b': 2, 'c': 3}
xx = mydict.get('a', 0) # 1 (key exists so paired value is returned)
yy = mydict.get('zzz', 0) # 0 (key does not exist so the
# default value is returned)
Ex. 6.22
The .keys() method gives access to the keys in a dict.
mydict = {'a': 1, 'b': 2, 'c': 3}
these_keys = mydict.keys()
for key in these_keys:
print(key)
print(list(these_keys)) # ['a', 'c', 'b']
The .values() method gives access to the values in a dict.
mydict = {'a': 1, 'b': 2, 'c': 3}
values = list(mydict.values()) # [1, 2, 3]
if 'c' in mydict.values():
print("'c' was found")
for value in mydict.values():
print(value)
.items() gives key/value pairs as 2-item tuples.
mydict = {'a': 1, 'b': 2, 'c': 3}
for key, value in mydict.items():
print(key, value) # a 1
# b 2
# c 3
print(list(mydict.items())) # [('a', 1), ('c', 3), ('b', 2)]
Ex. 6.23
dict items() can give us a list of 2-item tuples. dict() can convert this list back to a dictionary.
mydict = {'a': 1, 'b': 2, 'c': 3}
these_items = list(mydict.items()) # [('a', 1), ('c', 3), ('b', 2)]
some_items = these_items[0:3] # [('a', 1), ('c', 3)]
newdict = dict(some_items)
print(newdict) # {'a': 1, 'b': 2}
2-item tuples can be sorted and sliced, so they are a handy alternate structure.
zip() zips up parallel lists into tuples; dict() can convert this to dict.
list1 = ['a', 'b', 'c', 'd']
list2 = [ 1, 2, 3, 4 ]
tupes = list(zip(list1, list2))
print(tupes) # [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
print(dict(tupes)) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Occasionally we are faced with two lists that relate to each other one a 1-to-1 basis... or, we sometimes even shape our data into this form. Paralell lists like these can be zipped into multi-item tuples.