Py4Inf 09 Dictionaries
Py4Inf 09 Dictionaries
Chapter 9
$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on
darwin
>>> x = 2
>>> x = 4
>>> print x
4
A Story of Two Collections..
• List
• Dictionary
perfume
money
candy
http://en.wikipedia.org/wiki/Associative_array
Dictionaries
• Dictionaries are Python’s most powerful data collection
http://en.wikipedia.org/wiki/Associative_array
Dictionaries
• Lists index their entries
based on the position in >>> purse = dict()
>>> purse['money'] = 12
the list >>> purse['candy'] = 3
>>> purse['tissues'] = 75
• Dictionaries are like bags - >>> print purse
{'money': 12, 'tissues': 75, 'candy': 3}
no order >>> print purse['candy']
3
>>> purse['candy'] = purse['candy'] + 2
• So we index the things we >>> print purse
put in the dictionary with a {'money': 12, 'tissues': 75, 'candy': 5}
“lookup tag”
Comparing Lists and Dictionaries
• Dictionaries are like lists except that they use keys instead of
numbers to look up values
Dictionary
>>> ddd = dict()
>>> ddd['age'] = 21
>>> ddd['course'] = 182 Key Value
>>> print ddd
['course'] 182
{'course': 182, 'age': 21}
>>> ddd['age'] = 23 ddd
>>> print ddd ['age'] 21
{'course': 182, 'age': 23}
Dictionary Literals (Constants)
• Dictionary literals use curly braces and have a list of key : value pairs
• You can make an empty dictionary using empty curly braces
counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
if name not in counts:
counts[name] = 1
else :
counts[name] = counts[name] + 1
print counts
counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print counts
counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print counts
http://www.youtube.com/watch?v=EHJ9uYx5L58
Writing programs (or programming) is a very creative and rewarding activity. You can
write programs for many reasons ranging from making your living to solving a difficult
data analysis problem to having fun to helping someone else solve a problem. This book
assumes that everyone needs to know how to program and that once you know how to
program, you will figure out what you want to do with your newfound skills.
We are surrounded in our daily lives with computers ranging from laptops to cell phones.
We can think of these computers as our ''personal assistants'' who can take care of many
things on our behalf. The hardware in our current-day computers is essentially built to
continuously ask us the question, ''What would you like me to do next?''
Our computers are fast and have vasts amounts of memory and could be very helpful to
us if we only knew the language to speak to explain to the computer what we would like it
to ''do next''. If we knew this language we could tell the computer to do tasks on our
behalf that were repetitive. Interestingly, the kinds of things computers can do best are
often the kinds of things that we humans find boring and mind-numbing.
the clown ran after the car and the car ran into the tent and the
tent fell down on the clown and the car
Counting Pattern
counts = dict()
print 'Enter a line of text:' The general pattern to count the
line = raw_input('')
words in a line of text is to split
words = line.split() the line into words, then loop
through the words and use a
print 'Words:', words dictionary to track the count of
each word independently.
print 'Counting...'
for word in words:
counts[word] = counts.get(word,0) + 1
print 'Counts', counts
Counting Words
python wordcount.py
Enter a line of text:
the clown ran after the car and the car ran into the tent
and the tent fell down on the clown and the car
http://www.flickr.com/photos/71502646@N00/2526007974/
python wordcount.py
Enter a line of text:
the clown ran after the car and the car ran
counts = dict() into the tent and the tent fell down on
print 'Enter a line of text:' the clown and the car
line = raw_input('')
words = line.split()
Words: ['the', 'clown', 'ran', 'after', 'the',
print 'Words:', words 'car', 'and', 'the', 'car', 'ran', 'into', 'the',
print 'Counting...’
'tent', 'and', 'the', 'tent', 'fell', 'down', 'on',
for word in words: 'the', 'clown', 'and', 'the', 'car']
counts[word] = counts.get(word,0) + 1 Counting...
print 'Counts', counts