Download presentation
Presentation is loading. Please wait.
1
IST256 : Applications Programming for Information Systems
Dictionaries
2
Agenda Connection Activity Teaching: Practice Activity Dictionaries
Word Counts
3
Connect Activity 1-800 How’s My Teaching?
Please Fill Out Mid-Term Course Feedback: Share your feedback and enjoy an EXTRA Effort point!
4
Dictionaries The dict type is designed to store key-value pairs. In Python this is known as a mapping type. font={‘Name’:’Arial’,’Size’: 8} Python dicts are mutable which means you can change the values. Dictionary values are accessed by key not by index. font[‘Name’] = “Courier”
5
Watch Me Code Dictionary Basics: Create a dictionary Update its value
Print it out Short and sweet demo
6
Dictionary Functions Like str and list, the dict type has its own set of built- in functions. ping-types-dict
7
Watch Me Code Dictionary Functions: KeyError get() values() keys()
Short and sweet demo
8
90 Second Challenge Given: x = {"y":"w", "t":5, "q":True}
What is X["y"] ? What is X["w"] ? What is X["q"] ? What is X.get("w") ? What is X.values() ?
9
Help me Code List of Dictionary == In-Memory Database!!! Write a program to read customer information from a file (GitHub) and outputs the names and total purchased of NY customers only. Similar to this: file = '/pub/datasets/grades/fall2015.tsv' grades = list() with open(file) as inp: for line in inp: values = line.split() vald = { 'course': values[2], 'credits': values[3], 'grade' : values[4] } grades.append(vald)
10
Now You Code Statistical Word Counts
Write a program to read text from a file, and print out a distribution of words and their counts. Example: “This is what this is” would output: this:2 is:2 what:1 Suggested Approach Make a text file a paragraph of text in it. Or copy the text from the web. Write the program to read the file and print a line at a time Modify the program to split the line into words and print a word at a time. Modify the program to add the words in a dictionary using the word as they key and the count as the value. After the dictionary is created, write code to print out the dictionary.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.