INLS 560 – D ICTIONARIES Instructor: Jason Carter.

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

COLORING WITH NUMBERS. NumbersNumbers NumbersNumbers
Guide to Programming with Python
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.
© red ©
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
COLOURS.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Colors By Jes Betzold Red YellowBlue Orange PurpleGreen.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Homework Assignment You are going to research any artist of your choosing from any time period or genre. You are going to complete a one page double- spaced.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
If you say 8 color the ones in your picture purple. If you say 9 color the ones in your picture blue.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
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.
Monkey, Monkey In the Tree. Monkey, monkey in the tree Throw the yellow coconut down to me!
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
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.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
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.
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SETS AND DICTIONARIES CHAPTER Slides by James Tam Department of Computer Science, 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.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
When to use Tuples instead of Lists
Department of Computer Science,
Watch Pete the Cat here:
Colors/Color Words.
Guide to Programming with Python
Average Number of Photons
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
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
What Color is it?.
©
©
Topics Basic String Operations String Slicing
Topics Sequences Introduction to Lists List Slicing
Topics Basic String Operations String Slicing
CSE 231 Lab 8.
Introduction to Dictionaries
COMPUTER SCIENCE PRESENTATION.
Topics Basic String Operations String Slicing
Dictionary.
Tuple.
Presentation transcript:

INLS 560 – D ICTIONARIES Instructor: Jason Carter

T YPES int list string

D ICTIONARIES Dictionary: object that stores a collection of data Each element consists of a key and a value Often referred to as mapping of key to value Key must be an immutable object (cannot be changed!) To retrieve a specific value, use the key associated with it Format for creating a dictionary dictionary = { key1 : val1, key2 : val2 }

R ETRIEVING A V ALUE F ROM A D ICTIONARY Elements in dictionary are unsorted General format for retrieving value from dictionary: dictionary [ key ] If key in the dictionary, associated value is returned, otherwise, KeyError exception is raised Test whether a key is in a dictionary using the if/else statement and not in operator Helps prevent KeyError exceptions

R ETRIEVING A V ALUE FROM A D ICTIONARY

R ETRIEVING A L IST V ALUE FROM A D ICTIONARY

R ETRIEVING A L IST V ALUE FROM A D ICTIONARY - O UTPUT

R ETRIEVING A V ALUE FROM A D ICTIONARY – K EY E RROR

R ETRIEVING A V ALUE FROM A D ICTIONARY – K EY E RROR O UTPUT

C HECKING IF K EY E XISTS

C HECKING IF K EY E XISTS - O UTPUT

A DDING E LEMENTS TO AN E XISTING D ICTIONARY Dictionaries are mutable objects To add a new key-value pair: dictionary [ key ] = value If key exists in the dictionary, the value associated with it will be changed

A DDING E LEMENTS TO AN E XISTING D ICTIONARY

A DDING E LEMENTS TO AN E XISTING D ICTIONARY - O UTPUT

D ELETING E LEMENTS F ROM AN E XISTING D ICTIONARY To delete a key-value pair: del dictionary [ key ] If key is not in the dictionary, KeyError exception is raised

D ELETING E LEMENTS F ROM AN E XISTING D ICTIONARY

D ELETING E LEMENTS F ROM AN E XISTING D ICTIONARY - O UTPUT

U SING A D ICTIONARY AS A C OLOR M AP Dictionaries map one value to another We could map numbers to color names instead of names to phone #s, or to map numbers to images. Example 1: colors = {0: ‘white’, 1: ‘black’, 2: ‘pink’, 3: ‘purple’, 4: ‘red’, 5: ‘blue’, 6: ‘green’, 7: ‘orange’, 8: ‘yellow’} images = {0: ‘blank.gif’, 1: ‘a.gif’, 2: ‘b.gif’, 3: ‘c.gif’, 4: ‘d.gif’, 5: ‘e.gif’, 6: ‘f.gif’, 7: ‘g.gif’, 8: ‘h.gif’}

G ETTING THE N UMBER OF E LEMENTS AND M IXING D ATA T YPES len function: used to obtain number of elements in a dictionary Keys must be immutable objects, but associated values can be any type of object One dictionary can include keys of several different immutable types Values stored in a single dictionary can be of different types

C REATING AN E MPTY D ICTIONARY AND U SING FOR L OOP TO I TERATE O VER A D ICTIONARY To create an empty dictionary: Use {} Use built-in function dict() Elements can be added to the dictionary as program executes Use a for loop to iterate over a dictionary General format: for key in dictionary :

S OME D ICTIONARY M ETHODS get method: gets a value associated with specified key from the dictionary Format: dictionary.get( key, default ) default is returned if key is not found Alternative to [] operator Cannot raise KeyError exception keys method: returns all the dictionaries keys as a sequence Format: dictionary.keys()

S OME D ICTIONARY M ETHODS ( CONT ’ D.) values method: returns all the dictionaries values as a sequence Format: dictionary.values() Use a for loop to iterate over the values

S OME D ICTIONARY M ETHODS

S OME D ICTIONARY M ETHODS - O UTPUT

P RACTICE WITH D ICTIONARIES Write a program that opens the file text.txt (from Program 4) and counts the number of occurrences of each word. Print out each word and the number of times it occurs on a separate line. Loop through the dictionary to find the word that occurred the most – print that out. Hints: You will need to go through the file line by line and split the line – the split function returns a list – loop through that list of words and put them into your dictionary and keep track of how many times you saw each word.