Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See"— Presentation transcript:

1 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. Sets and Dictionaries

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

3 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?

4 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? Input is a list of several thousand bird names

5 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? Input is a list of several thousand bird names Output is a list of names and counts

6 Sets and DictionariesDictionaries Could use a list of [name, count] pairs

7 Sets and DictionariesDictionaries 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])

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

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

10 Sets and DictionariesDictionaries 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 pair already in the list

11 Sets and DictionariesDictionaries 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 bird we're looking for…

12 Sets and DictionariesDictionaries 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 its count and finish

13 Sets and DictionariesDictionaries 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 to the list

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

15 Sets and DictionariesDictionaries 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 []

16 Sets and DictionariesDictionaries 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]]

17 Sets and DictionariesDictionaries 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]]

18 Sets and DictionariesDictionaries 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]]

19 Sets and DictionariesDictionaries There's a better way

20 Sets and DictionariesDictionaries There's a better way Use a dictionary

21 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs

22 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs Like set elements, keys are:

23 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs Like set elements, keys are: - Immutable

24 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs Like set elements, keys are: - Immutable - Unique

25 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs Like set elements, keys are: - Immutable - Unique - Not stored in any particular order

26 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs Like set elements, keys are: - Immutable - Unique - Not stored in any particular order No restrictions on values

27 Sets and DictionariesDictionaries There's a better way Use a dictionary An unordered collection of key/value pairs Like set elements, keys are: - Immutable - Unique - Not stored in any particular order No restrictions on values - Don't have to be immutable or unique

28 Sets and DictionariesDictionaries Create a dictionary by putting key:value pairs in {}

29 Sets and DictionariesDictionaries >>> birthdays = {'Newton' : 1642, 'Darwin' : 1809} Create a dictionary by putting key:value pairs in {}

30 Sets and DictionariesDictionaries >>> birthdays = {'Newton' : 1642, 'Darwin' : 1809} Create a dictionary by putting key:value pairs in {} Retrieve values by putting key in []

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

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

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

34 Sets and DictionariesDictionaries Add another value by assigning to it

35 Sets and DictionariesDictionaries >>> birthdays['Turing'] = 1612 # that's not right Add another value by assigning to it

36 Sets and DictionariesDictionaries >>> birthdays['Turing'] = 1612 # that's not right Add another value by assigning to it Overwrite value by assigning to it as well

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

38 Sets and DictionariesDictionaries Note: entries are not in any particular order

39 Sets and DictionariesDictionaries 'Turing' 'Newton' 'Darwin' 1912 1642 1809 Note: entries are not in any particular order

40 Sets and DictionariesDictionaries Key must be in dictionary before use

41 Sets and DictionariesDictionaries >>> birthdays['Nightingale'] KeyError: 'Nightingale' Key must be in dictionary before use

42 Sets and DictionariesDictionaries >>> birthdays['Nightingale'] KeyError: 'Nightingale' Key must be in dictionary before use Test whether key is present using in

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

44 Sets and DictionariesDictionaries Use for to loop over keys

45 Sets and DictionariesDictionaries Use for to loop over keys Unlike lists, where for loops over values

46 Sets and DictionariesDictionaries >>> for name in birthdays:... print name, birthdays[name] Turing 1912 Newton 1642 Darwin 1809 Use for to loop over keys Unlike lists, where for loops over values

47 Sets and DictionariesDictionaries Let's count those birds

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

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

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

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

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

53 Sets and DictionariesDictionaries 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 doing to the next reader

54 Sets and DictionariesDictionaries 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 empty dictionary to fill

55 Sets and DictionariesDictionaries 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 values one at a time

56 Sets and DictionariesDictionaries 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 before processing

57 Sets and DictionariesDictionaries 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 have seen this value before…

58 Sets and DictionariesDictionaries 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 to its count

59 Sets and DictionariesDictionaries 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 time we have seen this name, store it with a count of 1

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

61 Sets and DictionariesDictionaries Counter in action

62 Sets and DictionariesDictionaries Counter in action start {}

63 Sets and DictionariesDictionaries Counter in action start {} loon {'loon' : 1}

64 Sets and DictionariesDictionaries Counter in action start {} loon {'loon' : 1} goose {'loon' : 1, 'goose' : 1}

65 Sets and DictionariesDictionaries Counter in action start {} loon {'loon' : 1} goose {'loon' : 1, 'goose' : 1} loon {'loon' : 2, 'goose' : 1}

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

67 July 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.


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

Similar presentations


Ads by Google