Python Mini-Course University of Oklahoma Department of Psychology Lesson 16 Dictionaries 5/10/09 Python Mini-Course: Lesson 16 1.

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Dictionaries: Keeping track of pairs
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
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.
CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Sequences The range function returns a sequence
Sequences A sequence is a list of elements Lists and tuples
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
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.
Python Data Structures
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.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
CSC1018F: Introduction to Python (Tutorial) James Gain Donald Cook
© 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.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 13 Case study: Word play 05/02/09 Python Mini-Course: Day 4 – Lesson.
Built-in Data Structures in Python An Introduction.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1.
Intro2cs Tirgul 5 1. What we will cover today?  File IO  Dictionaries  XML  Importing files  Function with unknown number of arguments  List comprehension.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Collections Michael Ernst CSE 190p University of Washington.
Head First Python: Ch 1. Everyone loves lists Aug 22, 2013 Hee-gook Jun.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
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.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
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
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
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.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Dictionaries Ruth Anderson UW CSE 160 Winter
CS2910 Week 6, Lab Today Dictionaries in Python SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
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.
Department of Computer Science,
Data types: Complex types (Dictionaries)
Section 6: Sequences Chapter 4 and 5.
Dictionaries, File operations
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
String and Lists Dr. José M. Reyes Álamo.
CSC1018F: Intermediate Python
Dictionaries Dictionary: object that stores a collection of data
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CHAPTER 4: Lists, Tuples and Dictionaries
Introduction to Dictionaries
Dictionary.
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Lesson 16 Dictionaries 5/10/09 Python Mini-Course: Lesson 16 1

Lesson objectives 1. Describe the characteristics of the dictionary data structure in Python 2. Perform basic operations with dictionaries including creation, copying, updating, and traversing 3. Use dictionaries in functions 5/10/09 Python Mini-Course: Lesson 16 2

The dictionary data structure In Python, a dictionary is mapping between a set of indices (keys) and a set of values The items in a dictionary are key- value pairs 5/10/09 Python Mini-Course: Lesson 16 3

The dictionary data structure Keys can be any Python data type Because keys are used for indexing, they should be immutable Values can be any Python data type Values can be mutable or immutable 5/10/09 Python Mini-Course: Lesson 16 4

Creating a dictionary eng2sp = dict() print eng2sp eng2sp['one'] = 'uno' print eng2sp eng2sp['two'] = 'dos' print eng2sp 5/10/09 Python Mini-Course: Lesson 16 5

Creating a dictionary eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} print eng2sp In general, the order of items in a dictionary is unpredictable Dictionaries are indexed by keys, not integers 5/10/09 Python Mini-Course: Lesson 16 6

Dictionary indexing print eng2sp['three'] print eng2sp['five'] * If the index is not a key in the dictionary, Python raises an exception 5/10/09 Python Mini-Course: Lesson 16 7

Dictionary indexing if 'five' in eng2sp: print eng2sp['five'] print eng2sp.get('five') 5/10/09 Python Mini-Course: Lesson 16 8

The in operator Note that the in operator works differently for dictionaries than for other sequences For offset indexed sequences (strings, lists, tuples), x in y checks to see whether x is an item in the sequence For dictionaries, x in y checks to see whether x is a key in the dictionary 5/10/09 Python Mini-Course: Lesson 16 9

Keys and values The keys method returns a list of the keys in a dictionary print eng2sp.keys() The values method returns a list of the values print eng2sp.values() 5/10/09 Python Mini-Course: Lesson 16 10

Keys and values The items method returns a list of tuple pairs of the key-value pairs in a dictionary print eng2sp.items() 5/10/09 Python Mini-Course: Lesson 16 11

Example: histogram.py def histogram(seq): d = dict() for element in seq: if element not in d: d[element] = 1 else: d[element] += 1 return d h = histogram('brontosaurus') print h 5/10/09 Python Mini-Course: Lesson 16 12

Example: histogram2.py Add the following code to histogram.py: def print_hist(hist): for key in hist: print key, hist[key] h = histogram('brontosaurus') print_hist(h) 5/10/09 Python Mini-Course: Lesson 16 13

Example: histogram2.py Change the print_hist function: def print_hist(hist): for key, value in hist: print key, value h = histogram('brontosaurus') print_hist(h) 5/10/09 Python Mini-Course: Lesson 16 14

Sorting the keys Change the print_hist function: def print_hist(hist): keys = hist.keys() keys.sort() for key in keys: print key, hist[key] h = histogram('brontosaurus') print_hist(h) 5/10/09 Python Mini-Course: Lesson 16 15

Using lists as values: invert.py Add the following code to histogram.py: def invert_dict(d): inv = dict() for key in d: val = d[key] if val not in inv: inv[val] = [key] else: inv[val].append(key) return inv 5/10/09 Python Mini-Course: Lesson 16 16

Using lists as values: invert.py Add the following code to histogram.py: hist = histogram('parrot') print hist inverted = invert_dict(hist) print inverted 5/10/09 Python Mini-Course: Lesson 16 17

Using tuples as keys: troupe.py troupe = {('Cleese', 'John'): [1,2,3], ('Chapman', 'Graham'): [4,5,6], ('Idle', 'Eric'): [7,8,9], ('Jones', 'Terry'): [10,11,12], ('Gilliam', 'Terry'): [13,14,15,16,17,18], ('Palin', 'Michael'): [19,20]} for last, first in troupe: print first, last, troupe[last, first] 5/10/09 Python Mini-Course: Lesson 16 18

Next session Handling program errors (exceptions) Reading and writing data files Sharing data with other programs (Excel, SAS, etc.) 5/10/09 Python Mini-Course: Lesson 16 19

Suggested exercises Exercise 12.4 The Case Study in chapter 13 5/10/09 Python Mini-Course: Lesson 16 20