33
Introduction to Python Dictionaries March 13, 2012 CS0931 - Intro. to Comp. for the Humanities and Social Sciences 1

Introduction to Python Dictionaries

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Python Dictionaries

Introduction to Python Dictionaries

March 13, 2012

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 1

Page 2: Introduction to Python Dictionaries

The Big Picture

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 2

Overall Goal Build a Concordance of a text

• Locations of words • Frequency of words

Today: Get Word Frequencies • Define the inputs and the outputs • Learn a new data structure • Write a function to get word frequencies • Go from word frequencies to a concordance (finally!)

Page 3: Introduction to Python Dictionaries

Word Frequency: Inputs and Outputs

I want to write a wordFreq function

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 3

The cat had a hat. The cat sat on the hat.

Page 4: Introduction to Python Dictionaries

Word Frequency: Inputs and Outputs

I want to write a wordFreq function

• What is the input to wordFreq?

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 4

The cat had a hat. The cat sat on the hat.

Page 5: Introduction to Python Dictionaries

Word Frequency: Inputs and Outputs

I want to write a wordFreq function

• What is the input to wordFreq?

• What is the output of wordFreq?

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 5

The cat had a hat. The cat sat on the hat.

Page 6: Introduction to Python Dictionaries

Word Frequency: Inputs and Outputs

I want to write a wordFreq function

• What is the input to wordFreq?

• What is the output of wordFreq?

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 6

The cat had a hat. The cat sat on the hat.

Word Freq.

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

Page 7: Introduction to Python Dictionaries

Word Frequency: Inputs and Outputs

I want to write a wordFreq function

• What is the input to wordFreq?

• What is the output of wordFreq?

• We could do this with a list. How?

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 7

The cat had a hat. The cat sat on the hat.

Word Freq.

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

Page 8: Introduction to Python Dictionaries

The Big Picture

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 8

Overall Goal Build a Concordance of a text

• Locations of words • Frequency of words

Today: Get Word Frequencies • Define the inputs and the outputs • Learn a new data structure • Write a function to get word frequencies • Go from word frequencies to a concordance (finally!)

Page 9: Introduction to Python Dictionaries

A New Data Structure

• A Data Structure is simply a way to store information.

– Lists are a type of data structure

• We can have lists of integers, floats, strings, booleans, or any combination.

• Organized linearly (indexed by a range of integers)

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 9

Page 10: Introduction to Python Dictionaries

A New Data Structure

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 10

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

Page 11: Introduction to Python Dictionaries

A New Data Structure

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 11

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

Associate each word with the frequency.

Page 12: Introduction to Python Dictionaries

A New Data Structure

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 12

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

Associate each word with the frequency.

Keys Values

Page 13: Introduction to Python Dictionaries

A New Data Structure

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 13

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

Associate each word with the frequency.

Keys Values

Key-Value Pairs

Key Value

‘the’ 3

‘cat’ 2

Page 14: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 14

Key Type Value Type Example Key

Example Value

Keys and values can be any type or data structure

Page 15: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 15

Key Type Value Type Example Key

Example Value

String Integer `the’ 3

String Integer `cat’ 2

Keys and values can be any type or data structure

Page 16: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 16

Key Type Value Type Example Key

Example Value

String Integer `the’ 3

String Integer `cat’ 2

String String `Alice’ `401-111-1111’

String String `Bob’ `401-222-2222’

Keys and values can be any type or data structure

Page 17: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 17

Key Type Value Type Example Key

Example Value

String Integer `the’ 3

String Integer `cat’ 2

String String `Alice’ `401-111-1111’

String String `Bob’ `401-222-2222’

Integer String 1 `one’

Integer String 2 `two’

Keys and values can be any type or data structure

Page 18: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 18

Key Type Value Type Example Key

Example Value

String Integer `the’ 3

String Integer `cat’ 2

String String `Alice’ `401-111-1111’

String String `Bob’ `401-222-2222’

Integer String 1 `one’

Integer String 2 `two’

Float List 3.0 [`t’, `h’, `r’, `e’, `e’]

Keys and values can be any type or data structure

Page 19: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 19

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

>>> freq = {}

>>> freq

{}

>>>

Initialize a Dictionary

Page 20: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 20

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

>>> freq = {}

>>> freq

{}

>>> freq['the'] = 3

>>> freq

{'the': 3}

>>>

Initialize a Dictionary

Key = ‘the’ Value = 3

Page 21: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 21

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

>>> freq = {}

>>> freq

{}

>>> freq['the'] = 3

>>> freq

{'the': 3}

>>> freq['cat'] = 2

>>> freq

{'the': 3, 'cat': 2}

>>>

Initialize a Dictionary

Key = ‘the’ Value = 3

Key = ‘cat’ Value = 2

Page 22: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 22

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

>>> freq2 = {'the':3,'cat':2}

>>> freq2

{'the': 3, 'cat': 2}

>>>

Initialize a dictionary with two key-value pairs

Page 23: Introduction to Python Dictionaries

A New Data Structure: Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 23

The cat had a hat. The cat sat on the hat.

Word Frequency

the 3

cat 2

had 1

a 1

hat 2

sat 1

on 1

>>> freq2 = {'the':3,'cat':2}

>>> freq2

{'the': 3, 'cat': 2}

>>>

>>> freq2['cat']

2

>>> freq2['the']

3

Retrieve a value using the key

Page 24: Introduction to Python Dictionaries

The Big Picture

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 24

Overall Goal Build a Concordance of a text

• Locations of words • Frequency of words

Today: Get Word Frequencies • Define the inputs and the outputs • Learn a new data structure • Write a function to get word frequencies • Go from word frequencies to a concordance (finally!)

Page 25: Introduction to Python Dictionaries

Python Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 25

Function (All operate on Dictionaries)

Input Output Example

keys() None List of keys

>>> freq2.keys()

['the', 'cat']

values() None List of values

>>> freq2.values()

[3, 2]

has_key(<key>) Key Boolean >>> freq2.has_key('missing')

False

<key> in <dict> (means same as above)

>>> 'the' in freq2

True

del(<dict>[<key>]) Dict. Entry

None >>> del(freq2[‘cat'])

Page 26: Introduction to Python Dictionaries

Python Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 26

Function (All operate on Dictionaries)

Input Output Example

keys() None List of keys

>>> freq2.keys()

['the', 'cat']

values() None List of values

>>> freq2.values()

[3, 2]

has_key(<key>) Key Boolean >>> freq2.has_key('missing')

False

<key> in <dict> (means same as above)

>>> 'the' in freq2

True

del(<dict>[<key>]) Dict. Entry

None >>> del(freq2[‘cat'])

• Keys Are Unique! • Dictionaries are Unordered! Unlike lists, which are linear.

Page 27: Introduction to Python Dictionaries

Break

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 27 http://xkcd.com/1007/

Page 28: Introduction to Python Dictionaries

The Big Picture

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 28

Overall Goal Build a Concordance of a text

• Locations of words • Frequency of words

Today: Get Word Frequencies • Define the inputs and the outputs • Learn a new data structure • Write a function to get word frequencies • Go from word frequencies to a concordance (finally!)

Page 29: Introduction to Python Dictionaries

Python Dictionaries

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 29

Function (All operate on Dictionaries)

Input Output Example

keys() None List of keys

>>> freq2.keys()

['the', 'cat']

values() None List of values

>>> freq2.values()

[3, 2]

has_key(<key>) Key True or False

>>> freq2.has_key('missing')

False

<key> in <dict> (means same as above)

>>> 'the' in freq2

True

del(<dict>[<key>]) Dict. Entry

None >>> del(freq2[‘cat'])

• Keys Are Unique! • Dictionaries are Unordered! Unlike lists, which are linear.

Page 30: Introduction to Python Dictionaries

The Big Picture

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 30

Overall Goal Build a Concordance of a text

• Locations of words • Frequency of words

Today: Get Word Frequencies • Define the inputs and the outputs • Learn a new data structure • Write a function to get word frequencies • Go from word frequencies to a concordance (finally!)

Page 31: Introduction to Python Dictionaries

Building a Concordance

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 31

The cat had a hat. The cat sat on the hat.

0 1 2 3 4 5 6 7 8 9 10

Word List of Positions Frequency

the [0,5,9] 3

cat [1,6] 2

had [2] 1

a [3] 1

hat [4,10] 2

sat [7] 1

on [8] 1

Page 32: Introduction to Python Dictionaries

The Big Picture

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 32

Overall Goal Build a Concordance of a text

• Locations of words • Frequency of words

Today: Get Word Frequencies • Define the inputs and the outputs • Learn a new data structure • Write a function to get word frequencies • Go from word frequencies to a concordance (finally!)

This is part of your HW

Page 33: Introduction to Python Dictionaries

Project 1

Go to the Projects page on the class website

• Make sure that your website shows up properly.

• Look through a few other people’s projects

CS0931 - Intro. to Comp. for the Humanities and Social Sciences 33