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

Lesson 16 Dictionaries

  • Upload
    kort

  • View
    53

  • Download
    0

Embed Size (px)

DESCRIPTION

Lesson 16 Dictionaries. Python Mini-Course University of Oklahoma Department of Psychology. Lesson objectives. Describe the characteristics of the dictionary data structure in Python Perform basic operations with dictionaries including creation, copying, updating, and traversing - PowerPoint PPT Presentation

Citation preview

Page 1: Lesson 16 Dictionaries

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Lesson 16Dictionaries

5/10/09Python Mini-Course: Lesson 161

Page 2: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 162

Page 3: Lesson 16 Dictionaries

The dictionary data structure

In Python, a dictionary is mapping between a set of indices (keys) and a set of valuesThe items in a dictionary are key-value pairs

5/10/09Python Mini-Course: Lesson 163

Page 4: Lesson 16 Dictionaries

The dictionary data structure

Keys can be any Python data typeBecause keys are used for indexing, they should be immutable

Values can be any Python data typeValues can be mutable or immutable

5/10/09Python Mini-Course: Lesson 164

Page 5: Lesson 16 Dictionaries

Creating a dictionary

eng2sp = dict()

print eng2sp

eng2sp['one'] = 'uno'

print eng2sp

eng2sp['two'] = 'dos'

print eng2sp

5/10/09Python Mini-Course: Lesson 165

Page 6: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 166

Page 7: Lesson 16 Dictionaries

Dictionary indexing

print eng2sp['three']

print eng2sp['five']

* If the index is not a key in the dictionary, Python raises an exception

5/10/09Python Mini-Course: Lesson 167

Page 8: Lesson 16 Dictionaries

Dictionary indexing

if 'five' in eng2sp: print eng2sp['five']

print eng2sp.get('five')

5/10/09Python Mini-Course: Lesson 168

Page 9: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 169

Page 10: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1610

Page 11: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1611

Page 12: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1612

Page 13: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1613

Page 14: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1614

Page 15: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1615

Page 16: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1616

Page 17: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1617

Page 18: Lesson 16 Dictionaries

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/09Python Mini-Course: Lesson 1618

Page 19: Lesson 16 Dictionaries

Next session

Handling program errors (exceptions)

Reading and writing data filesSharing data with other programs (Excel, SAS, etc.)

5/10/09Python Mini-Course: Lesson 1619

Page 20: Lesson 16 Dictionaries

Suggested exercises

Exercise 12.4The Case Study in chapter 13

5/10/09Python Mini-Course: Lesson 1620