nov 1
Dictionaries
person = { 'name' : 'Jim',
'age' : 47,
'hair' : 'black',
}
print person['name']
Like an array, but we use strings rather than numbers.
- unordered
- The parts are called keys and values.
'key' : 'value'
- key is always a string
- value can be anything - a list, an object, a number, ...
- Extremely powerful way to save arbitrary sorts of data.
Discuss
- interating over a dictionary
- accessing a key that hasn't been defined yet
- using dictionaries to count things
- compare and contrast dictionary with class
- example: building a tree out of nodes
In class
- write a program to find word frequencies in Moby Dick
- Google 'gutenberg moby dick'