14. dictionaries and sets rocky k. c. chang 17 november 2014 (based on from charles dierbach,...

18
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)

Upload: lester-little

Post on 06-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 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

14. DICTIONARIES AND SETSRocky K. C. Chang17 November 2014

(Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and Enbody, The Practice of Computing Using Python)

Page 2: 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

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

Page 3: 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

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.

Page 4: 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

EXERCISE 14.1Try•nicknames = dict()•print(nicknames)•nicknames = {"Rocky" : "CHANG Kow Chuen", "Memory" : "CHIU Wai Han"}•print(nicknames)•nicknames["Dennis"] = "LIU Yan Wang"•print(nicknames)

Page 5: 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

EXERCISE 14.2Try•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.

Page 6: 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

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"].

Page 7: 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

EXERCISE 14.3Create 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)?

Page 8: 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

EXERCISE 14.4Create 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.

Page 9: 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

Dictionary operators

Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

Page 10: 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

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.

Page 11: 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

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.

Page 12: 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

EXERCISE 14.6Go 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.

Page 13: 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

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().

Page 14: 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

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.

Page 15: 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

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.

Page 16: 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

Set operators

Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

Page 17: 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

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).

Page 18: 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

END