Lesson 16 Dictionaries Python MiniCourse University of Oklahoma

  • Slides: 20
Download presentation
Lesson 16 Dictionaries Python Mini-Course University of Oklahoma Department of Psychology 1 Python Mini-Course:

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

Lesson objectives 1. Describe the characteristics of the dictionary data structure in Python 2.

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 2 Python Mini-Course: Lesson 16 5/10/09

The dictionary data structure In Python, a dictionary is mapping between a set of

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 3 Python Mini-Course: Lesson 16 5/10/09

The dictionary data structure Keys can be any Python data type Because keys are

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 4 Python Mini-Course: Lesson 16 5/10/09

Creating a dictionary eng 2 sp = dict() print eng 2 sp['one'] = 'uno'

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

Creating a dictionary eng 2 sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} print

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

Dictionary indexing print eng 2 sp['three'] print eng 2 sp['five'] * If the index

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

Dictionary indexing if 'five' in eng 2 sp: print eng 2 sp['five'] print eng

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

The in operator • Note that the in operator works differently for dictionaries than

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 9 Python Mini-Course: Lesson 16 5/10/09

Keys and values The keys method returns a list of the keys in a

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

Keys and values The items method returns a list of tuple pairs of the

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

Example: histogram. py def histogram(seq): d = dict() for element in seq: if element

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 12 Python Mini-Course: Lesson 16 5/10/09

Example: histogram 2. py Add the following code to histogram. py: def print_hist(hist): for

Example: histogram 2. 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) 13 Python Mini-Course: Lesson 16 5/10/09

Example: histogram 2. py Change the print_hist function: def print_hist(hist): for key, value in

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

Sorting the keys Change the print_hist function: def print_hist(hist): keys = hist. keys() keys.

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) 15 Python Mini-Course: Lesson 16 5/10/09

Using lists as values: invert. py Add the following code to histogram. py: def

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 16 Python Mini-Course: Lesson 16 5/10/09

Using lists as values: invert. py Add the following code to histogram. py: hist

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

Using tuples as keys: troupe = {('Cleese', 'John'): ('Chapman', 'Graham'): ('Idle', 'Eric'): ('Jones', 'Terry'):

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

Next session Handling program errors (exceptions) Reading and writing data files Sharing data with

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

Suggested exercises Exercise 12. 4 The Case Study in chapter 13 20 Python Mini-Course:

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