Top Banner
Dictionaries Sets and Dictionaries Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
67

Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Aug 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Dictionaries

Sets and Dictionaries

Dictionaries

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Page 2: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Back to the data from our summer counting birds in

a mosquito-infested swamp in northern Ontario

Sets and Dictionaries Dictionaries

Page 3: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Back to the data from our summer counting birds in

a mosquito-infested swamp in northern Ontario

How many birds of each kind did we see?How many birds of each kind did we see?

Sets and Dictionaries Dictionaries

Page 4: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Back to the data from our summer counting birds in

a mosquito-infested swamp in northern Ontario

How many birds of each kind did we see?How many birds of each kind did we see?

Input is a list of several thousand bird names

Sets and Dictionaries Dictionaries

Page 5: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Back to the data from our summer counting birds in

a mosquito-infested swamp in northern Ontario

How many birds of each kind did we see?How many birds of each kind did we see?

Input is a list of several thousand bird names

Output is a list of names and counts

Sets and Dictionaries Dictionaries

Page 6: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

Sets and Dictionaries Dictionaries

Page 7: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

Sets and Dictionaries Dictionaries

Page 8: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

List of pairsifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

List of pairs

Sets and Dictionaries Dictionaries

Page 9: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

Name to addifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

Name to add

Sets and Dictionaries Dictionaries

Page 10: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

Look at each pairifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1)

Look at each pair

already in the list

Sets and Dictionaries Dictionaries

Page 11: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

If this is the bird

we're looking for…

Sets and Dictionaries Dictionaries

Page 12: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

…add 1 to its

count and finish

Sets and Dictionaries Dictionaries

Page 13: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1]) Otherwise, add

a new pair to

Sets and Dictionaries Dictionaries

the list

Page 14: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

Pattern: handle an existing case and return in loop,

Sets and Dictionaries Dictionaries

Pattern: handle an existing case and return in loop,

or take default action if we exit the loop normally

Page 15: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

start []

Sets and Dictionaries Dictionaries

start []

Page 16: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

start []

Sets and Dictionaries Dictionaries

start []

loon [['loon', 1]]

Page 17: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

start []

Sets and Dictionaries Dictionaries

start []

loon [['loon', 1]]

goose [['loon', 1], ['goose', 1]]

Page 18: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Could use a list of [name, count] pairs

defdefdefdef another_bird(counts, bird_name):

forforforfor i inininin rangerangerangerange(lenlenlenlen(counts)):

ifififif counts[i][0] == bird_name:

counts[i][1] += 1

returnreturnreturnreturn

counts.append([bird_name, 1])

start []

Sets and Dictionaries Dictionaries

start []

loon [['loon', 1]]

goose [['loon', 1], ['goose', 1]]

loon [['loon', 2], ['goose', 1]]

Page 19: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Sets and Dictionaries Dictionaries

Page 20: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

Sets and Dictionaries Dictionaries

Page 21: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Sets and Dictionaries Dictionaries

Page 22: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Like set elements, keys are:

Sets and Dictionaries Dictionaries

Page 23: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Like set elements, keys are:

- Immutable

Sets and Dictionaries Dictionaries

Page 24: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Like set elements, keys are:

- Immutable

- Unique

Sets and Dictionaries Dictionaries

Page 25: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Like set elements, keys are:

- Immutable

- Unique

- Not stored in any particular order

Sets and Dictionaries Dictionaries

- Not stored in any particular order

Page 26: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Like set elements, keys are:

- Immutable

- Unique

- Not stored in any particular order

Sets and Dictionaries Dictionaries

- Not stored in any particular order

No restrictions on values

Page 27: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

There's a better way

Use a dictionary

An unordered collection of key/value pairsAn unordered collection of key/value pairs

Like set elements, keys are:

- Immutable

- Unique

- Not stored in any particular order

Sets and Dictionaries Dictionaries

- Not stored in any particular order

No restrictions on values

- Don't have to be immutable or unique

Page 28: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Create a dictionary by putting key:value pairs in {}

Sets and Dictionaries Dictionaries

Page 29: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays = {'Newton' : 1642, 'Darwin' : 1809}

Create a dictionary by putting key:value pairs in {}

Sets and Dictionaries Dictionaries

Page 30: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays = {'Newton' : 1642, 'Darwin' : 1809}

Create a dictionary by putting key:value pairs in {}

Retrieve values by putting key in []Retrieve values by putting key in []

Sets and Dictionaries Dictionaries

Page 31: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays = {'Newton' : 1642, 'Darwin' : 1809}

Create a dictionary by putting key:value pairs in {}

Retrieve values by putting key in []Retrieve values by putting key in []

Just like indexing strings and lists

Sets and Dictionaries Dictionaries

Page 32: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays = {'Newton' : 1642, 'Darwin' : 1809}

Create a dictionary by putting key:value pairs in {}

Retrieve values by putting key in []

>>> print birthdays['Newton']

1642

Retrieve values by putting key in []

Just like indexing strings and lists

Sets and Dictionaries Dictionaries

Page 33: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays = {'Newton' : 1642, 'Darwin' : 1809}

Create a dictionary by putting key:value pairs in {}

Retrieve values by putting key in []

>>> print birthdays['Newton']

1642

Retrieve values by putting key in []

Just like indexing strings and lists

Just like using a phonebook or dictionary

Sets and Dictionaries Dictionaries

Just like using a phonebook or dictionary

Page 34: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Add another value by assigning to it

Sets and Dictionaries Dictionaries

Page 35: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays['Turing'] = 1612 # that's not right

Add another value by assigning to it

Sets and Dictionaries Dictionaries

Page 36: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays['Turing'] = 1612 # that's not right

Add another value by assigning to it

Overwrite value by assigning to it as wellOverwrite value by assigning to it as well

Sets and Dictionaries Dictionaries

Page 37: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays['Turing'] = 1612 # that's not right

Add another value by assigning to it

Overwrite value by assigning to it as well

>>> birthdays['Turing'] = 1912

>>> print birthdays

{'Turing' : 1912, 'Newton' : 1642, 'Darwin' : 1809}

Overwrite value by assigning to it as well

Sets and Dictionaries Dictionaries

Page 38: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Note: entries are not in any particular order

Sets and Dictionaries Dictionaries

Page 39: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

'Turing'

Note: entries are not in any particular order

'Turing'

'Newton'

'Darwin'1912

1642

1809

Sets and Dictionaries Dictionaries

1642

Page 40: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Key must be in dictionary before use

Sets and Dictionaries Dictionaries

Page 41: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays['Nightingale']

KeyError: 'Nightingale'

Key must be in dictionary before use

Sets and Dictionaries Dictionaries

Page 42: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays['Nightingale']

KeyError: 'Nightingale'

Key must be in dictionary before use

Test whether key is present using in

Sets and Dictionaries Dictionaries

Page 43: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> birthdays['Nightingale']

KeyError: 'Nightingale'

Key must be in dictionary before use

>>> 'Nightingale' in birthdays

False

>>> 'Darwin' in birthdays

True

Test whether key is present using in

Sets and Dictionaries Dictionaries

True

Page 44: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Use for to loop over keys

Sets and Dictionaries Dictionaries

Page 45: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Use for to loop over keys

Unlike lists, where for loops over values

Sets and Dictionaries Dictionaries

Page 46: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

>>> for name in birthdays:

Use for to loop over keys

Unlike lists, where for loops over values

>>> for name in birthdays:

... print name, birthdays[name]

Turing 1912

Newton 1642

Darwin 1809

Sets and Dictionaries Dictionaries

Page 47: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Let's count those birds

Sets and Dictionaries Dictionaries

Page 48: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

importimportimportimport sys

Let's count those birds

ifififif __name__ == '__main__':

reader = openopenopenopen(sys.argv[1], 'r')

lines = reader.readlines()

reader.close()

count = count_names(lines)

Sets and Dictionaries Dictionaries

forforforfor name inininin count:

printprintprintprint name, count[name]

Page 49: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

importimportimportimport sys

Let's count those birds

ifififif __name__ == '__main__':

reader = openopenopenopen(sys.argv[1], 'r')

lines = reader.readlines()

reader.close()

count = count_names(lines)

Read all the data

Sets and Dictionaries Dictionaries

forforforfor name inininin count:

printprintprintprint name, count[name]

Page 50: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

importimportimportimport sys

Let's count those birds

ifififif __name__ == '__main__':

reader = openopenopenopen(sys.argv[1], 'r')

lines = reader.readlines()

reader.close()

count = count_names(lines) Count distinct values

Sets and Dictionaries Dictionaries

forforforfor name inininin count:

printprintprintprint name, count[name]

Count distinct values

Page 51: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

importimportimportimport sys

Let's count those birds

ifififif __name__ == '__main__':

reader = openopenopenopen(sys.argv[1], 'r')

lines = reader.readlines()

reader.close()

count = count_names(lines)

Sets and Dictionaries Dictionaries

forforforfor name inininin count:

printprintprintprint name, count[name]

Show results

Page 52: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

Page 53: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {} Explain what we're doingresult = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

Explain what we're doing

to the next reader

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

Page 54: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {} Create an emptyresult = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

Create an empty

dictionary to fill

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

Page 55: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

Handle input values

one at a time

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

Page 56: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

Clean up before

processing

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

Page 57: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

If we have

seen this value

before…

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

before…

Page 58: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

…add one to

its count

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

its count

Page 59: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse: But if it's the first time

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result

But if it's the first time

we have seen this name,

store it with a count of 1

Page 60: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

defdefdefdef count_names(lines):

'''Count unique lines of text, returning dictionary.'''

result = {}result = {}

forforforfor name inininin lines:

name = name.strip()

ifififif name inininin result:

result[name] = result[name] + 1

elseelseelseelse:

Sets and Dictionaries Dictionaries

result[name] = 1

returnreturnreturnreturn result Return the result

Page 61: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Counter in action

Sets and Dictionaries Dictionaries

Page 62: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Counter in action

start {}

Sets and Dictionaries Dictionaries

Page 63: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Counter in action

start {}

loon {'loon' : 1}

Sets and Dictionaries Dictionaries

Page 64: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Counter in action

start {}

loon {'loon' : 1}

goose {'loon' : 1, 'goose' : 1}

Sets and Dictionaries Dictionaries

Page 65: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Counter in action

start {}

loon {'loon' : 1}

goose {'loon' : 1, 'goose' : 1}

loon {'loon' : 2, 'goose' : 1}

Sets and Dictionaries Dictionaries

Page 66: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

Counter in action

start {}

loon {'loon' : 1}

goose {'loon' : 1, 'goose' : 1}

loon {'loon' : 2, 'goose' : 1}

But like sets, dictionaries are much more efficient

than lookup lists

Sets and Dictionaries Dictionaries

Page 67: Dictionaries - v4.software-carpentry.org · Sets and Dictionaries Dictionaries. Back to the data from our summer counting birds in a mosquito-infested swamp in northern Ontario How

July 2010

created by

Greg Wilson

July 2010

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.