Download presentation
Presentation is loading. Please wait.
Published byLester Little Modified over 9 years ago
1
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and Enbody, The Practice of Computing Using Python)
2
Objectives To learn two Python data models: dictionary and set To apply these two data models to solve problems, such as counting the frequency of words
3
Storing students' records Problem: How to store student's data in a program after reading the data from a file? A possible way is to store the data in a list. However, a list can be indexed only by non-negative integers, but ours should be indexed by names. Map the names to a set of integers. Use the integers as indices for accessing the list. A much better solution is to use a Python dictionary that maps the set of names (keys) directly to a set of values.
4
EXERCISE 14.1 Try nicknames = dict() print(nicknames) nicknames = {"Rocky" : "CHANG Kow Chuen", "Memory" : "CHIU Wai Han"} print(nicknames) nicknames["Dennis"] = "LIU Yan Wang" print(nicknames)
5
EXERCISE 14.2 Try Add an integer-typed index and an integer- typed value. Add an integer-typed index and a list-typed value. Add a list-typed index and a list-typed value.
6
Python dictionary A dictionary is a mutable, associative data structure of variable length. The key can be of any immutable type. The values can be of any type and are unordered. An example: dicta = {'rocky': 'CHANG Kow Chuen', ('a', 'b', 'c'): [1, 2, 3], 1: 2} ('a', 'b', 'c') is called a tuple, an immutable list type. divmod(x,y) returns a tuple. Get the value using its index, e.g., dicta["rocky"].
7
EXERCISE 14.3 Create a dictionary of dictionary, such as dicta = {"a":1, "b":{"aa":11, "bb":22}}. How do you get the values in the inner dictionary (i.e., 11 and 22)?
8
EXERCISE 14.4 Create a dictionary dicta and try len(dicta) Use the keyword "in" to check whether an object is a key in dicta. Use a for loop to print out all the keys in dicta. Use a for loop to print out all the keys and the corresponding values on a new line.
9
Dictionary operators Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
10
Dictionary methods Python provides a number of other methods that iterate through the elements in a dictionary. items(): all the key-values pairs as a list of tuples keys(): all the keys as a list values(): all the values as a list The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes. They are iterable, therefore can be used in a loop.
11
EXERCISE 14.5 Create a dictionary and use the three methods and a loop to print out their keys only, values only, and key-value.
12
EXERCISE 14.6 Go to your Q1 of A6. Modify your program so that it will count the number of occurrences of each word in a file and store them in a dictionary. Print the key-value on a new line.
13
EXERCISE 14.7 In this exercise, we would like to print out the statistics in a sorted order of the words. In order to do this, you need to create a list of tuples first for the key-value in the dictionary and then apply sorted().
14
Sets in Python A set is collection of objects which are elements of the set. The elements could be of any types. There is no order in a set. There are no duplicate elements in a set. {} is an empty set.
15
EXERCISE 14.8 Create a set using set() and {} with duplicate elements. Create an empty set. Use len() to get the size. Use the keyword "in" to test whether an object is an element of a set. Use a for loop to print all the elements of a set.
16
Set operators Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
17
EXERCISE 14.9 Try all four set operations. You can also use four methods for sets: intersection, union, difference, symmetric_difference. For example, set_A.union(set_B).
18
END
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.