Pythonlearn 09 Dictionaries
Pythonlearn 09 Dictionaries
What is a Collection?
• A collection is nice because we can put more than one value in it
Python Dictionaries and carry them all around in one convenient package
1 2
3 4
10/01/21
Dictionaries Dictionaries
• Dictionaries are Python’s most powerful data collection
tissue
calculator • Dictionaries allow us to do fast database-like operations in Python
5 6
7 8
10/01/21
>>> ddd = dict() >>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
Dictionary
>>> ddd['age'] = 21 >>> print(jjj)
>>> ddd['course'] = 182 Key Value {'jan': 100, 'chuck': 1, 'fred': 42}
>>> print(ddd)
>>> ooo = { }
{'course': 182, 'age': 21} ['course'] 182
>>> ddd['age'] = 23 ddd >>> print(ooo)
>>> print(ddd) ['age'] 21 {}
{'course': 182, 'age': 23} >>>
9 10
11 12
10/01/21
13 14
15 16
10/01/21
17 18
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
19 20
10/01/21
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 Counting Pattern
problem to having fun to helping someone else solve a problem. This book assumes that
counts = dict()
everyone needs to know how to program and that once you know how to program, you will figure
print('Enter a line of text:')
out what you want to do with your newfound skills. The general pattern to count the
line = input('')
We are surrounded in our daily lives with computers ranging from laptops to cell phones. We words in a line of text is to split
can think of these computers as our “personal assistants” who can take care of many things on words = line.split() the line into words, then loop
our behalf. The hardware in our current-day computers is essentially built to continuously ask us through the words and use a
the question, “What would you like me to do next?” print('Words:', words) dictionary to track the count of
Our computers are fast and have vast amounts of memory and could be very helpful to us if we each word independently.
only knew the language to speak to explain to the computer what we would like it to do next. If print('Counting...')
we knew this language we could tell the computer to do tasks on our behalf that were repetitive. for word in words:
Interestingly, the kinds of things computers can do best are often the kinds of things that we counts[word] = counts.get(word,0) + 1
humans find boring and mind-numbing. print('Counts', counts)
21 22
python wordcount.py
python wordcount.py counts = dict() Enter a line of text:
Enter a line of text: line = input('Enter a line of text:') the clown ran after the car and the car ran
the clown ran after the car and the car ran into the tent words = line.split()
into the tent and the tent fell down on the
and the tent fell down on the clown and the car
print('Words:', words) clown and the car
Words: ['the', 'clown', 'ran', 'after', 'the', 'car', print('Counting...’)
'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and', Words: ['the', 'clown', 'ran', 'after', 'the', 'car',
'the', 'tent', 'fell', 'down', 'on', 'the', 'clown', for word in words: 'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
'and', 'the', 'car'] counts[word] = counts.get(word,0) + 1 'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
print('Counts', counts)
Counting… '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, Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3,
'tent': 2} 'into': 1, 'after': 1, 'clown': 2, 'down': 1, 'fell':
1, 'the': 7, 'tent': 2}
http://www.flickr.com/photos/71502646@N00/2526007974/
23 24
10/01/21
25 26
27 28
10/01/21
29 30