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.

Slides:



Advertisements
Similar presentations
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dictionaries: Keeping track of pairs
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.
Python Dictionary.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
October 17, 2005ICP: Chapter 5: Lists and Dictionaries 1 Introduction to Computer Programming Chapter 5: Lists and Dictionaries Michael Scherger Department.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
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.
Chapter 9 Dictionaries and Sets.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Collections Michael Ernst CSE 190p University of Washington.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
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.
Introduction to Python Aug 22, 2013 Hee-gook Jun.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
CS2910 Week 6, Lab Today Dictionaries in Python SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SETS AND DICTIONARIES CHAPTER Slides by James Tam Department of Computer Science, University.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
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.
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
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
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CHAPTER THREE Sequences.
6. Lists Let's Learn Python and Pygame
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to Dictionaries
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
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
Topics Sequences Introduction to Lists List Slicing
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:

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)

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

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.

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

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

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.

Dictionary operators Source: Charles Dierbach 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 14.5 Create a dictionary and use the three methods and a loop to print out their keys only, values only, and key-value.

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.

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

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.

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.

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

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

END