6. Dictionaries and sets Rocky K. C. Chang 18 October 2018

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
CS2910 Week 6, Lab Today Dictionaries in Python SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Advanced Python Idioms
Data types: Complex types (List)
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
Data types: Complex types (Dictionaries)
Dictionaries, File operations
Lecture 10 Data Collections
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
Introduction to Strings
Introduction to Strings
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
CSc 110, Spring 2018 Lecture 33: Dictionaries
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
4. sequence data type Rocky K. C. Chang 16 September 2018
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Introduction to Dictionaries
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Algorithmic complexity: Speed of algorithms
Topics Sequences Introduction to Lists List Slicing
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
Introduction to Strings
Advanced Python Idioms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Algorithmic complexity: Speed of algorithms
Advanced Python Idioms
15-110: Principles of Computing
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
CSE 231 Lab 8.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CSE 231 Lab 9.
Introduction to Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Tuple.
Introduction to Computer Science
Presentation transcript:

6. Dictionaries and sets Rocky K. C. Chang 18 October 2018 (Based on from Charles Dierbach, Introduction to Computer Science Using Python, and Punch and Enbody, The Practice of Computing Using Python)

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

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.

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

Exercise 6.2 Try Add an integer-typed key and an integer-typed value. Add an integer-typed key and a list-typed value. Add a list-typed key and a list-typed value.

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} divmod(x,y) returns a tuple. Get the value using its index, e.g., dicta["rocky"].

Exercise 6.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)?

Exercise 6.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.

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

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.

Exercise 6.5 Create a dictionary and use the three methods and a loop to print out their keys only, values only, and key-value.

Exercise 6.6 Write a program that 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. Assume that the file does not contain any punctuations, symbols, and numbers.

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

Exercise 6.8 Given a dictionary d and a key k, it is easy to find the corresponding value v = d[k]. This operation is called a lookup. Write a function for reverse lookup. That is, given d and a value v, the function will return the first key that maps to v.

Exercise 6.9 Modify reverse_lookup() in exercise 6.8 so that it builds and returns a list of all keys that map to v, or an empty list if there are none.

Sets in Python A set is collection of non-mutable objects which are elements of the set. Set is mutable. 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.

Exercise 6.10 Create a set using set() and {} with duplicate elements. Create an empty set. Create a set of a list as an element. 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.

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

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

END