0% found this document useful (0 votes)
8 views32 pages

09 Python - Dictionaries

Uploaded by

sampleraipur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views32 pages

09 Python - Dictionaries

Uploaded by

sampleraipur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Python Dictionaries

Chapter 9

[email protected]
What is a Collection?

• A collection is nice because we can put more than one value in


it and carry them all around in one convenient package

• We have a bunch of values in a single “variable”

• We do this by having more than one place “in” the variable

• We have ways of finding the different places in the variable

[email protected]
What is not a “Collection”
• Most of our variables have one value in them - when we put a new
value in the variable - the old value is overwritten

>>> x = 2
>>> x = 4
>>> print(x)
4

[email protected]
A Story of Two Collections..
• List

> A linear collection of values that stay in order

• Dictionary

> A “bag” of values, each with its own label

[email protected]
Dictionaries
tissue
calculator

perfume
money
candy

http://en.wikipedia.org/wiki/Associative_array
[email protected]
Dictionaries
• Dictionaries are Python’s most powerful data collection

• Dictionaries allow us to do fast database-like operations in Python

• Dictionaries have different names in different languages

> Associative Arrays - Perl / PHP

> Properties or Map or HashMap - Java

> Property Bag - C# / .Net

http://en.wikipedia.org/wiki/Associative_array
[email protected]
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”

[email protected]
Comparing Lists and Dictionaries
• Dictionaries are like lists except that they use keys instead of
numbers to look up values

>>> lst = list() >>> ddd = dict()


>>> lst.append(21) >>> ddd['age'] = 21
>>> lst.append(183) >>> ddd['course'] = 182
>>> print(lst) >>> print(ddd)
[21, 183] {'course': 182, 'age': 21}
>>> lst[0] = 23 >>> ddd['age'] = 23
>>> print(lst) >>> print(ddd)
[23, 183] {'course': 182, 'age': 23}

[email protected]
>>> lst = list()
>>> lst.append(21) List
>>> lst.append(183) Key Value
>>> print(lst)
[0] 21
[21, 183]
>>> lst[0] = 23
lst
[1] 183
>>> print(lst)
[23, 183]

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}
[email protected]
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

>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> print(jjj)
{'jan': 100, 'chuck': 1, 'fred': 42}
>>> ooo = { }
>>> print(ooo)
{}
>>>

[email protected]
[email protected]
Most Common Name?

marquard cwen cwen


zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
[email protected]
Most Common Name?

[email protected]
Most Common Name?

marquard cwen cwen


zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
[email protected]
Most Common Name?

marquard cwen cwen


zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
[email protected]
Many Counters with a Dictionary
• One common use of dictionary is Key Value
counting how often we “see” something

>>> ccc = dict()


>>> ccc['csev'] = 1
>>> ccc['cwen'] = 1
>>> print(ccc)
{'csev': 1, 'cwen': 1}
>>> ccc['cwen'] = ccc['cwen'] + 1
>>> print(ccc)
{'csev': 1, 'cwen': 2}

[email protected]
Dictionary Tracebacks
• It is an error to reference a key which is not in the dictionary

• We can use the in operator to see if a key is in the dictionary

>>> ccc = dict()


>>> print(ccc['csev'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'csev'
>>> print('csev' in ccc)
False

[email protected]
When we see a new name
• When we encounter a new name, we need to add a new entry in the
dictionary and if this the second or later time we have seen the name,
we simply add one to the count in the dictionary under that name

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)

{'csev': 2, 'zqian': 1, 'cwen': 2}


[email protected]
The get method for dictionaries
• This pattern of checking to see
if a key is already in a if name in counts:
x = counts[name]
dictionary and assuming a else :
default value if the key is not x = 0
there is so common, that there
is a method called get() that
does this for us x = counts.get(name, 0)

Default value if key does not exist


(and no Traceback). {'csev': 2, 'zqian': 1, 'cwen': 2}
[email protected]
Simplified counting with get()
• We can use get() and provide a default value of zero when the key is
not yet in the dictionary - and then just add one

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print(counts)

Default {'csev': 2, 'zqian': 1, 'cwen': 2}


[email protected]
Simplified counting with get()

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
[email protected]
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.
[email protected]
the clown ran after the car and the car ran into the tent and the
tent fell down on the clown and the car

[email protected]
Counting Pattern
counts = dict()
line = input('Enter a line of text:')
The general pattern to count the
words = line.split() words in a line of text is to split
the line into words, then loop
print('Words:', words)
through the words and use a
print('Counting...') dictionary to track the count of
for word in words: each word independently.
counts[word] = counts.get(word,0) + 1
print('Counts', counts)

[email protected]
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

Words: ['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…

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3, 'into': 1,


'after': 1, 'clown': 2, 'down': 1, 'fell': 1, 'the': 7,
'tent': 2}

http://www.flickr.com/photos/71502646@N00/2526007974/
[email protected]
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
line = input('Enter a line of text:') the clown and the car
words = line.split()

print('Words:', words)
Words: ['the', 'clown', 'ran', 'after', 'the',
print('Counting...') 'car', 'and', 'the', 'car', 'ran', 'into', 'the',
'tent', 'and', 'the', 'tent', 'fell', 'down', 'on',
for word in words:
counts[word] = counts.get(word,0) + 1
'the', 'clown', 'and', 'the', 'car']
print('Counts', counts) Counting...

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3,


'into': 1, 'after': 1, 'clown': 2, 'down': 1, 'fell': 1,
'the': 7, 'tent': 2}

[email protected]
Definite Loops and Dictionaries
• Even though dictionaries are not stored in order, we can write a for
loop that goes through all the entries in a dictionary - actually it goes
through all of the keys in the dictionary and looks up the values
>>> counts = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
>>> for key in counts:
... print(key, counts[key])
...
jan 100
chuck 1
fred 42
>>>
[email protected]
Retrieving lists of Keys and Values
>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
>>> print(list(jjj))
• You can get a list of ['jan', 'chuck', 'fred']
keys, values, or >>> print(jjj.keys())
['jan', 'chuck', 'fred']
items (both) from a >>> print(jjj.values())
dictionary [100, 1, 42]
>>> print(jjj.items())
[('jan', 100), ('chuck', 1), ('fred', 42)]
>>>

What is a 'tuple'? - coming soon...


[email protected]
Bonus: Two Iteration Variables!
• We loop through the
key-value pairs in a >>> jjj = { 'chuck' : 1 , 'fred' : 42,
'jan': 100}
dictionary using *two* >>> for aaa,bbb in jjj.items() :
iteration variables ... print(aaa, bbb)
...
• Each iteration, the first jan 100
variable is the key and chuck 1 aaa bbb
fred 42
the second variable is >>> [jan] 100
the corresponding value
[chuck] 1
for the key
[fred] 42
[email protected]
name = input('Enter file:')
handle = open(name)
text = handle.read() python words.py
words = text.split() Enter file: words.txt
counts = dict()
to 16
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None python words.py


bigword = None
for word,count in counts.items(): Enter file: clown.txt
if bigcount is None or count > bigcount: the 7
bigword = word
bigcount = count

print(bigword, bigcount)

[email protected]
Summary

[email protected]
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors or translation credits here

You might also like