COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

Guide to Programming with Python
Fundamentals of Python: From First Programs Through Data Structures
Python Dictionary.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.
Built-in Data Structures in Python An Introduction.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 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.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
Arrays.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Do Now: Perform the indicated operation. 1.). Algebra II Elements 11.1: Matrix Operations HW: HW: p.590 (16-36 even, 37, 44, 46)
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.
COMPSCI 107 Computer Science Fundamentals
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Computer Programming BCT 1113
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,
Matrix Operations.
Data types: Complex types (Dictionaries)
LING 388: Computers and Language
CMSC201 Computer Science I for Majors Lecture 20 – Dictionaries
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Coding Concepts (Data Structures)
String and Lists Dr. José M. Reyes Álamo.
Sparse matrices, hash tables, cell arrays
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Introduction to Dictionaries
Data Structures (CS212D) Week # 2: Arrays.
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
Arrays Week 2.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CIS16 Application Development and Programming using Visual Basic.net
Dictionaries: Keeping track of pairs
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Bryan Burlingame Halloween 2018
Chapter 4 Matrices & Determinants
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Introduction to Dictionaries
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Sample lecture slides.
Dictionary.
Tuple.
Presentation transcript:

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu Fall 2016 1 1

THE ONLINE BOOK CHAPTER XII DICTIONARIES

Motivation Suppose we have a list of phone numbers such as: [[ 'CS', '713-743-3350'],[ 'UHPD' : '713-743-3333']] or inventory of soda cans such as: [['Coke', 12], ['Diet Coke', 8], ['Coke Zero', 2]] Finding the phone number of UHPD or the number of cans of 'Coke Zero' we have in stock requires searching through the list Too slow

Dictionaries (I) Store pairs of entries called items { 'CS' : '713-743-3350', 'UHPD' : '713-743-3333'} Each pair of entries contains A key A value Key and values are separated by a colon Pairs of entries are separated by commas Dictionary is enclosed within curly braces

Dictionaries (II) { 'CS' : '713-743-3350', 'UHPD' : '713-743-3333'} Can also be built step by step: number = {} number['CS'] = '713-743-3350' number['UHPD'] = '713-743-3333'

Accessing values If we have age = {'Alice' : 25, 'Bob': 28} then age['Alice'] is 25 and age['Bob'] is 28

A problem What is printed by the following program? mydict = {"cat":12, "dog":6, "horse":23} print(mydict["dog"]) 12 6 23 Error

Dictionaries are mutable >>> age = {'Alice' : 25, 'Bob' : 28} >>> saved = age >>> age['Bob'] = 29 >>> age {'Bob': 29, 'Alice': 25} >>> saved {'Bob': 29, 'Alice': 25}

Keys must be unique >>> age = {'Alice' : 25, 'Bob' : 28, 'Alice' : 26} >>> age {'Bob': 28, 'Alice': 26}

Operations on dictionaries >>> age = {'Bob': 28, 'Alice': 26} >>> len(age) 2 >>>age['Bob'] += 1 >>>age {'Bob': 29, 'Alice': 26} >>>age['Bob'] + age ['Alice'] 55

Adding contents age = {} age['Alice'] = 25 Cannot do the same with lists alist=[] alist[10]= 5 # NO NO

Global dictionary methods Arguments Description keys() none Returns a view of the keys in the dictionary values() Returns a view of the values in the dictionary items() Returns a view of the items (KV pairs) in the dictionary

Examples >>> age = {'Alice' : 25, 'Carol': 'twenty-two'} >>> age.items() dict_items([ ('Alice', 25), ('Carol', 'twenty-two')]) >>> age.keys() dict_keys([ 'Alice', 'Carol']) age.values() dict_values([25, 'twenty-two'])

Views and lists (I) To represent mydictionary as a list of KV tuples mydictionary.items() To get the keys mydictionary.keys() To get the values mydictionary.values() Cannot index these three representations They are views, no lists

Views and lists (II) Can convert view into lists of tuples list() function age = {'Alice' : 25, 'Carol': 22} >>> age.items() dict_items([('Carol', 22), ('Alice', 25)]) >>> list(age.items()) [('Carol', 'twenty-two'), ('Alice', 25)]

More dictionary methods Arguments Description get() key Returns value associated with the key or none otherwise update() KV pair Updates/adds a KV pair popitem() Returns value associated with the key and removes the KV pair from the list none Same for arbitrary KV pair

Updating dictionaries >>> age = {'Alice': 26 , 'Carol' : 22} >>> age.update({'Bob' : 29}) >>> age {'Bob': 29, 'Carol': 22, 'Alice': 26} >>> age.update({'Carol' : 23}) >>> age {'Bob': 29, 'Carol': 23, 'Alice': 26} >>> age['Bob'] = 30 >>> age {'Bob': 30, 'Carol': 23, 'Alice': 26}

Returning a value >>> age = {'Bob': 29,'Liz': 23,'Ann': 26} >>> age.get('Bob') 29 >>> age['Bob'] 29

Removing a specific item (I) >>> a = {'Ann' : 24, 'Liz' : 'twenty-two'} >>> a {'Liz': 'twenty-two', 'Ann': 24} >>> a.pop('Liz') 'twenty-two' >>> a {'Ann': 24}

Removing a specific item (II) >>> a.pop('Alice') 26 >>> a {} >>>

Remove a random item >>> age = {'Bob': 29, 'Liz': 22, 'Ann': 27} >>> age.popitem() ('Bob', 29) >>> age {'Liz': 22, 'Ann': 27} >>> age.popitem() ('Liz', 23) >>> age {'Ann': 27}

Traversing a dictionary Suppose we have an inventory such as: cans = {'Coke':12, 'Diet Coke':8, 'Coke Zero' : 2} How can we compute the total number of cans we have? Must go through all items in the dictionary Can use for akey in cans.keys() to go though all keys

Example cans = {'Coke':12, 'Diet Coke':8, 'Coke Zero':2} total = 0 for akey in cans.keys() : total += cans[akey] print(total, 'cans of soft drink') will print 22 cans of soft drink

Algebra matrices A matrix such as 𝐴= 0 1 2 10 11 12 20 21 22 can be represented as a list of lists a=[[0, 1, 2],[10, 11, 12],[20, 21, 22]] with row and column indices starting at zero The element a[i][j] of list a then corresponds to the element 𝑎 𝑖𝑗 of the matrix

Sparse matrices (I) Consider a matrix like D= 1 0 0 0 1 0 0 0 1 that has few non-zero elements The conventional Python representation d = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] wastes a lot of space Becomes a problem for very large matrices

Sparse matrices (II) We could use a dictionary that would only contains the non-zero elements of the matrix We would then use d[(i, i)] instead of d[i][i] to access the non-zero elements of the diagonal matrix

How to access all elements d[(i, i)] only works for non-zero elements on the diagonal Use alternate version of get() method dict.get(key, alternate) where alternate specifies the value to return if key is not in dictionary dict d.get((i, j), 0) works for all elements of the matrix