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

Dictionaries Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Embed Size (px)

DESCRIPTION

Sets and DictionariesDictionaries 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?

Citation preview

Page 1: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Dictionaries

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution LicenseSee http://software-carpentry.org/license.html for more information.

Sets and Dictionaries

Page 2: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Back to the data from our summer counting birds ina mosquito-infested swamp in northern Ontario

Page 3: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Back to the data from our summer counting birds ina mosquito-infested swamp in northern OntarioHow many birds of each kind did we see?

Page 4: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Back to the data from our summer counting birds ina mosquito-infested swamp in northern OntarioHow many birds of each kind did we see?Input is a list of several thousand bird names

Page 5: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Back to the data from our summer counting birds ina mosquito-infested swamp in northern OntarioHow many birds of each kind did we see?Input is a list of several thousand bird namesOutput is a list of names and counts

Page 6: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs

Page 7: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

Page 8: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

List of pairs

Page 9: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

Name to add

Page 10: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1)

Look at each pairalready in the list

Page 11: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

If this is the birdwe're looking for…

Page 12: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

…add 1 to itscount and finish

Page 13: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])Otherwise, add

a new pair tothe list

Page 14: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

Pattern: handle an existing case and return in loop,or take default action if we exit the loop normally

Page 15: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

start []

Page 16: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

start []loon [['loon', 1]]

Page 17: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

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

Page 18: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Could use a list of [name, count] pairs def another_bird(counts, bird_name): for i in range(len(counts)): if counts[i][0] == bird_name: counts[i][1] += 1 return counts.append([bird_name, 1])

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

Page 19: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better way

Page 20: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionary

Page 21: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairs

Page 22: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairsLike set elements, keys are:

Page 23: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairsLike set elements, keys are:- Immutable

Page 24: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairsLike set elements, keys are:- Immutable- Unique

Page 25: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairsLike set elements, keys are:- Immutable- Unique- Not stored in any particular order

Page 26: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairsLike set elements, keys are:- Immutable- Unique- Not stored in any particular orderNo restrictions on values

Page 27: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

There's a better wayUse a dictionaryAn unordered collection of key/value pairsLike set elements, keys are:- Immutable- Unique- Not stored in any particular orderNo restrictions on values- Don't have to be immutable or unique

Page 28: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

Page 29: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

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

Page 30: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

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

Retrieve values by putting key in []

Page 31: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

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

Retrieve values by putting key in []Just like indexing strings and lists

Page 32: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

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

>>> print birthdays['Newton']1642

Retrieve values by putting key in []Just like indexing strings and lists

Page 33: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

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

>>> print birthdays['Newton']1642

Retrieve values by putting key in []Just like indexing strings and lists

Just like using a phonebook or dictionary

Page 34: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Add another value by assigning to it

Page 35: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

Add another value by assigning to it

Page 36: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

Add another value by assigning to it

Overwrite value by assigning to it as well

Page 37: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

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

Add another value by assigning to it

>>> birthdays['Turing'] = 1912>>> print birthdays{'Turing' : 1912, 'Newton' : 1642, 'Darwin' : 1809}

Overwrite value by assigning to it as well

Page 38: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Note: entries are not in any particular order

Page 39: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

'Turing'

'Newton'

'Darwin'1912

1642

1809

Note: entries are not in any particular order

Page 40: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Key must be in dictionary before use

Page 41: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

>>> birthdays['Nightingale']KeyError: 'Nightingale'

Key must be in dictionary before use

Page 42: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

>>> birthdays['Nightingale']KeyError: 'Nightingale'

Key must be in dictionary before use

Test whether key is present using in

Page 43: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

>>> birthdays['Nightingale']KeyError: 'Nightingale'

Key must be in dictionary before use

>>> 'Nightingale' in birthdaysFalse>>> 'Darwin' in birthdaysTrue

Test whether key is present using in

Page 44: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Use for to loop over keys

Page 45: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Use for to loop over keysUnlike lists, where for loops over values

Page 46: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

>>> for name in birthdays:... print name, birthdays[name]

Turing 1912Newton 1642Darwin 1809

Use for to loop over keysUnlike lists, where for loops over values

Page 47: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Let's count those birds

Page 48: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

import sys

if __name__ == '__main__': reader = open(sys.argv[1], 'r') lines = reader.readlines() reader.close() count = count_names(lines) for name in count: print name, count[name]

Let's count those birds

Page 49: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

import sys

if __name__ == '__main__': reader = open(sys.argv[1], 'r') lines = reader.readlines() reader.close() count = count_names(lines) for name in count: print name, count[name]

Let's count those birds

Read all the data

Page 50: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

import sys

if __name__ == '__main__': reader = open(sys.argv[1], 'r') lines = reader.readlines() reader.close() count = count_names(lines) for name in count: print name, count[name]

Let's count those birds

Count distinct values

Page 51: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

import sys

if __name__ == '__main__': reader = open(sys.argv[1], 'r') lines = reader.readlines() reader.close() count = count_names(lines) for name in count: print name, count[name]

Let's count those birds

Show results

Page 52: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

Page 53: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

Explain what we're doingto the next reader

Page 54: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

Create an emptydictionary to fill

Page 55: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

Handle input valuesone at a time

Page 56: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

Clean up beforeprocessing

Page 57: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

If we haveseen this valuebefore…

Page 58: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

…add one toits count

Page 59: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result

But if it's the first timewe have seen this name,store it with a count of 1

Page 60: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

def count_names(lines): '''Count unique lines of text, returning dictionary.'''

result = {} for name in lines: name = name.strip() if name in result: result[name] = result[name] + 1 else: result[name] = 1

return result Return the result

Page 61: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Counter in action

Page 62: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Counter in actionstart {}

Page 63: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Counter in actionstart {}loon {'loon' : 1}

Page 64: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Counter in actionstart {}loon {'loon' : 1}goose {'loon' : 1, 'goose' : 1}

Page 65: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Counter in actionstart {}loon {'loon' : 1}goose {'loon' : 1, 'goose' : 1}loon {'loon' : 2, 'goose' : 1}

Page 66: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Sets and Dictionaries Dictionaries

Counter in actionstart {}loon {'loon' : 1}goose {'loon' : 1, 'goose' : 1}loon {'loon' : 2, 'goose' : 1}

But like sets, dictionaries are much more efficientthan lookup lists

Page 67: Dictionaries Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

July 2010

created by

Greg Wilson

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution LicenseSee http://software-carpentry.org/license.html for more information.