Transcript for Wednesday 07 November (dictionary)
| categories: Transcripts
Transcript for today.
# IPython log file
# create an empty dictionary
d = {}
# create a dictionary with 3 key:value pairs
d = { 1:"hi there", 2:"ho ho", 42:"the answer"}
d
#[Out]# {1: 'hi there', 2: 'ho ho', 42: 'the answer'}
# ask for the value associated with the key 1
d[1]
#[Out]# 'hi there'
# or key 42
d[42]
#[Out]# 'the answer'
# but we get an error if we ask for key 3 which isn't present
d[3]
# we can test to see if a key is present
42 in d
#[Out]# True
3 in d
#[Out]# False
# btw, in also works for lists. But in the case of lists it has to compare to each item
# for a dictionary it takes constant time regardless of the number of entries
L = [ 1, 2, 42 ]
1 in L
#[Out]# True
3 in L
#[Out]# False
# we can get just the keys
d.keys()
#[Out]# [1, 2, 42]
# we can add a new key
d[17] = True
d
#[Out]# {1: 'hi there', 2: 'ho ho', 17: True, 42: 'the answer'}
d.keys()
#[Out]# [1, 2, 42, 17]
# we can get just the values
d.values()
#[Out]# ['hi there', 'ho ho', 'the answer', True]
# we can get both the keys and the values
d.items()
#[Out]# [(1, 'hi there'), (2, 'ho ho'), (42, 'the answer'), (17, True)]
# keys don't have to be numbers
d['gary'] = 58
d.keys()
#[Out]# [1, 2, 'gary', 42, 17]
# we can print the contents in sorted order by sorting the keys
for key in sorted(d.keys()):
print key, d[key]
# at this point we wrote a function to count items using a dictionalry
# I'll include the source here but we wrote it in the editor and used run to load it.
def count(input):
d = {} # initialize an empty dictionary
for i in input: # iterate over the items in the input
if i not in d: # if the item is not in the dictionary
d[i] = 0 # add it with a count of 0
d[i] = d[i] + 1 # add 1 to the number of times we've seen this item
return d # return the dictionary
# use it to count the occurances of letters in a string
count('now is the time for all good men')
#[Out]# {' ': 7,
#[Out]# 'a': 1,
#[Out]# 'd': 1,
#[Out]# 'e': 3,
#[Out]# 'f': 1,
#[Out]# 'g': 1,
#[Out]# 'h': 1,
#[Out]# 'i': 2,
#[Out]# 'l': 2,
#[Out]# 'm': 2,
#[Out]# 'n': 2,
#[Out]# 'o': 4,
#[Out]# 'r': 1,
#[Out]# 's': 1,
#[Out]# 't': 2,
#[Out]# 'w': 1}
# we can use split on a string to break it into a list of words
s = 'now is the time for all good men'
s.split()
#[Out]# ['now', 'is', 'the', 'time', 'for', 'all', 'good', 'men']
# we can count integers, for example we could roll a die
r = np.random.random_integers(1, 6, 1000)
r
#[Out]# array([3, 2, 6, 2, 4, 1, 4, 3, 4, 2, 1, 6, 6, 6, 3, 4, 1, 2, 6, 3, 1, 3, 4,
#[Out]# 6, 3, 1, 1, 5, 6, 5, 4, 6, 1, 2, 6, 2, 2, 4, 2, 6, 3, 2, 3, 2, 5, 6,
#[Out]# 5, 4, 6, 3, 2, 5, 1, 4, 3, 1, 3, 3, 2, 6, 5, 4, 2, 6, 5, 3, 5, 5, 4,
#[Out]# 2, 3, 1, 5, 5, 3, 4, 2, 3, 2, 5, 2, 3, 2, 2, 5, 4, 3, 1, 4, 4, 4, 1,
#[Out]# 1, 1, 3, 4, 3, 5, 3, 4, 5, 3, 4, 5, 5, 6, 1, 6, 5, 2, 4, 3, 4, 5, 5,
#[Out]# 1, 6, 5, 2, 1, 3, 6, 1, 4, 4, 2, 3, 6, 4, 3, 4, 2, 3, 1, 1, 1, 4, 5,
#[Out]# 3, 3, 6, 1, 5, 2, 3, 3, 5, 1, 2, 5, 5, 4, 6, 3, 3, 3, 6, 5, 5, 1, 1,
#[Out]# 3, 5, 2, 2, 4, 5, 3, 6, 3, 2, 6, 5, 6, 3, 6, 6, 2, 3, 2, 2, 5, 5, 6,
#[Out]# 3, 4, 1, 1, 3, 6, 4, 6, 3, 6, 4, 2, 4, 1, 3, 2, 6, 6, 3, 6, 1, 1, 4,
#[Out]# 2, 1, 4, 1, 4, 1, 6, 5, 1, 5, 5, 3, 6, 1, 2, 4, 3, 3, 2, 3, 6, 2, 6,
#[Out]# 3, 5, 6, 1, 1, 5, 5, 1, 2, 6, 3, 3, 3, 2, 1, 3, 5, 6, 1, 4, 3, 4, 5,
#[Out]# 3, 5, 6,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, 2, 6, 5, 3, 4, 3, 1, 1, 6, 3, 4, 2, 2, 1, 5, 3, 5, 4, 4, 3, 3,
#[Out]# 2, 6, 6, 4, 4, 1, 6, 3, 5, 1, 5, 6, 1, 6, 4, 5, 3, 1, 2, 2, 1, 1, 1,
#[Out]# 3, 3, 1, 6, 2, 6, 3, 1, 1, 5, 6, 3, 5, 2, 3, 4, 4, 2, 1, 2, 2, 6, 5,
#[Out]# 3, 4, 2, 6, 4, 5, 1, 1, 4, 5, 4, 6, 3, 4, 2, 5, 5, 6, 6, 3, 2, 1, 2,
#[Out]# 2, 6, 3, 5, 3, 1, 4, 4, 5, 6, 3, 1, 2, 2, 3, 5, 1, 6, 2, 1, 3, 4, 1,
#[Out]# 5, 1, 2, 3, 1, 4, 5, 1, 3, 6, 4])
count(r)
#[Out]# {1: 169, 2: 152, 3: 180, 4: 171, 5: 160, 6: 168}
# remember our original dictionary?
d
#[Out]# {1: 'hi there', 2: 'ho ho', 17: True, 42: 'the answer', 'gary': 58}
# we can delete a key
del d['gary']
# now gary is gone.
d
#[Out]# {1: 'hi there', 2: 'ho ho', 17: True, 42: 'the answer'}