Sets and Dictionaries Examples Copyright Software Carpentry 2010

  • Slides: 47
Download presentation
Sets and Dictionaries Examples Copyright © Software Carpentry 2010 This work is licensed under

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

How early was each kind of bird seen? Sets and Dictionaries Examples

How early was each kind of bird seen? Sets and Dictionaries Examples

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04 ⋮ Sets and Dictionaries 05: 38 06: 02 06: 07 05: 09 05: 29 ⋮ loon goose loon ostrich loon ⋮ # hallucinating? Examples

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04 ⋮ 05: 38 06: 02 06: 07 05: 09 05: 29 ⋮ loon goose loon ostrich loon ⋮ # hallucinating? Want the minimum of all times associated with a bird Sets and Dictionaries Examples

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04 ⋮ 05: 38 06: 02 06: 07 05: 09 05: 29 ⋮ loon goose loon ostrich loon ⋮ # hallucinating? Want the minimum of all times associated with a bird Use bird name as dictionary key Sets and Dictionaries Examples

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04

How early was each kind of bird seen? 2010 -07 -03 2010 -07 -04 ⋮ 05: 38 06: 02 06: 07 05: 09 05: 29 ⋮ loon goose loon ostrich loon ⋮ # hallucinating? Want the minimum of all times associated with a bird Use bird name as dictionary key And earliest observation time as value Sets and Dictionaries Examples

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader =

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader = open(filename, 'r') result = [] for line in reader: fields = line. split('#')[0]. strip(). split() assert len(fields) == 3, 'Bad line "%s"' % line result. append(fields) return result Sets and Dictionaries Examples

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader =

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader = open(filename, 'r') result = [] Setup for line in reader: fields = line. split('#')[0]. strip(). split() assert len(fields) == 3, 'Bad line "%s"' % line result. append(fields) return result Sets and Dictionaries Examples

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader =

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader = open(filename, 'r') result = [] for line in reader: Get data from each line of the file fields = line. split('#')[0]. strip(). split() assert len(fields) == 3, 'Bad line "%s"' % line result. append(fields) return result Sets and Dictionaries Examples

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader =

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader = open(filename, 'r') result = [] for line in reader: fields = line. split('#')[0]. strip(). split() assert len(fields) == 3, 'Bad line "%s"' % line result. append(fields) return result Check that the data might be right Sets and Dictionaries Examples

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader =

def read_observations(filename): '''Read data, return [(date, time, bird). . . ]. ''' reader = open(filename, 'r') result = [] for line in reader: fields = line. split('#')[0]. strip(). split() assert len(fields) == 3, 'Bad line "%s"' % line result. append(fields) return result Sets and Dictionaries Store it Examples

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for (date, time, bird) in data: if bird not in result: result[bird] = time else: result[bird] = min(result[bird], time) return result Sets and Dictionaries Examples

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for (date, time, bird) in data: if bird not in result: result[bird] = time Setup else: result[bird] = min(result[bird], time) return result Sets and Dictionaries Examples

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for (date, time, bird) in data: if bird not in result: result[bird] = time Process each tuple in turn else: result[bird] = min(result[bird], time) return result Sets and Dictionaries Examples

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for (date, time, bird) in data: First sighting, if bird not in result: so this must be result[bird] = time earliest time else: result[bird] = min(result[bird], time) return result Sets and Dictionaries Examples

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for

def earliest_observation(data): '''How early did we see each bird? ''' result = {} for (date, time, bird) in data: Subsequent if bird not in result: sighting, so result[bird] = time take minimum else: result[bird] = min(result[bird], time) return result Sets and Dictionaries Examples

What birds were seen on each day? Sets and Dictionaries Examples

What birds were seen on each day? Sets and Dictionaries Examples

What birds were seen on each day? Very similar structure. . . Sets and

What birds were seen on each day? Very similar structure. . . Sets and Dictionaries Examples

What birds were seen on each day? Very similar structure. . . but use

What birds were seen on each day? Very similar structure. . . but use a set to record one or more birds, rather than taking the minimum time Sets and Dictionaries Examples

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for (date, time, bird) in data: if date not in result: result[date] = set() result[date]. add(bird) return result Sets and Dictionaries Examples

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for (date, time, bird) in data: if date not in result: result[date] = set() Setup result[date]. add(bird) return result Sets and Dictionaries Examples

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for (date, time, bird) in data: if date not in result: result[date] = set() Process each tuple in turn result[date]. add(bird) return result Sets and Dictionaries Examples

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for (date, time, bird) in data: Make sure there if date not in result: is space to record result[date] = set() result[date]. add(bird) this bird return result Sets and Dictionaries Examples

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for

def birds_by_date(data): '''Which birds were seen on each day? ''' result = {} for (date, time, bird) in data: if date not in result: result[date] = set() result[date]. add(bird) Record this bird return result Sets and Dictionaries Examples

Sets and Dictionaries Examples

Sets and Dictionaries Examples

2010 -07 -03 05: 38 loon '2010 -07 -03' 'loon' Sets and Dictionaries Examples

2010 -07 -03 05: 38 loon '2010 -07 -03' 'loon' Sets and Dictionaries Examples

2010 -07 -03 05: 38 06: 02 loon goose '2010 -07 -03' 'goose' Sets

2010 -07 -03 05: 38 06: 02 loon goose '2010 -07 -03' 'goose' Sets and Dictionaries 'loon' Examples

2010 -07 -03 05: 38 06: 02 06: 07 loon goose loon '2010 -07

2010 -07 -03 05: 38 06: 02 06: 07 loon goose loon '2010 -07 -03' 'goose' Sets and Dictionaries 'loon' Examples

2010 -07 -03 2010 -07 -04 05: 38 06: 02 06: 07 05: 09

2010 -07 -03 2010 -07 -04 05: 38 06: 02 06: 07 05: 09 '2010 -07 -03' 'goose' Sets and Dictionaries loon goose loon ostrich # hallucinating? '2010 -07 -04' 'loon' 'ostrich' Examples

2010 -07 -03 2010 -07 -04 05: 38 06: 02 06: 07 05: 09

2010 -07 -03 2010 -07 -04 05: 38 06: 02 06: 07 05: 09 05: 29 '2010 -07 -03' loon goose loon ostrich loon # hallucinating? '2010 -07 -04' 'loon' 'goose' Sets and Dictionaries 'loon' 'ostrich' Examples

Which bird did we see least frequently? Sets and Dictionaries Examples

Which bird did we see least frequently? Sets and Dictionaries Examples

Which bird did we see least frequently? Actually, which birds, since two or more

Which bird did we see least frequently? Actually, which birds, since two or more could be tied for the low score Sets and Dictionaries Examples

Which bird did we see least frequently? Actually, which birds, since two or more

Which bird did we see least frequently? Actually, which birds, since two or more could be tied for the low score Two-pass algorithm Sets and Dictionaries Examples

Which bird did we see least frequently? Actually, which birds, since two or more

Which bird did we see least frequently? Actually, which birds, since two or more could be tied for the low score Two-pass algorithm - Find the minimum value in the dictionary Sets and Dictionaries Examples

Which bird did we see least frequently? Actually, which birds, since two or more

Which bird did we see least frequently? Actually, which birds, since two or more could be tied for the low score Two-pass algorithm - Find the minimum value in the dictionary - Find all keys with that value Sets and Dictionaries Examples

Which bird did we see least frequently? Actually, which birds, since two or more

Which bird did we see least frequently? Actually, which birds, since two or more could be tied for the low score Two-pass algorithm - Find the minimum value in the dictionary - Find all keys with that value Combine these calculations in a one-pass algorithm Sets and Dictionaries Examples

Which bird did we see least frequently? Actually, which birds, since two or more

Which bird did we see least frequently? Actually, which birds, since two or more could be tied for the low score Two-pass algorithm - Find the minimum value in the dictionary - Find all keys with that value Combine these calculations in a one-pass algorithm Assume we already have a dictionary counts recording how often each kind of bird was seen Sets and Dictionaries Examples

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number =

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number = 0 for bird in counts: …handle this bird… return result Sets and Dictionaries Examples

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number =

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number = 0 for bird in counts: …handle this bird… return result if len(result) == 0: result = {bird} number = counts[bird] elif counts[bird] < number: result = {bird} number = counts[bird] elif counts[bird] == number: result. add(bird) Sets and Dictionaries Examples

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number =

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number = 0 for bird in counts: …handle this bird… return result if len(result) == 0: result = {bird} number = counts[bird] elif counts[bird] < number: result = {bird} number = counts[bird] elif counts[bird] == number: result. add(bird) Case 1: first bird (initializing data structures) Sets and Dictionaries Examples

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number =

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number = 0 for bird in counts: …handle this bird… return result if len(result) == 0: result = {bird} number = counts[bird] elif counts[bird] < number: result = {bird} number = counts[bird] elif counts[bird] == number: result. add(bird) Case 2: new minimum, so replace everything Sets and Dictionaries Examples

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number =

def least_frequently_seen(counts): '''Which bird(s) were least frequently seen? ''' result = set() number = 0 for bird in counts: …handle this bird… return result if len(result) == 0: result = {bird} number = counts[bird] elif counts[bird] < number: result = {bird} number = counts[bird] elif counts[bird] == number: result. add(bird) Case 3: tied equal for minimum Sets and Dictionaries Examples

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 0 result

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 0 result Before the loop Sets and Dictionaries Examples

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 3 'loon'

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 3 'loon' result Case 1: first bird (initializing data structures) Sets and Dictionaries Examples

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 1 result

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 1 result 'goose' Case 2: new minimum, so replace everything Sets and Dictionaries Examples

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 1 'ostrich'

{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 1 'ostrich' result 'goose' Case 3: tied equal for minimum Sets and Dictionaries Examples

created by Greg Wilson July 2010 Copyright © Software Carpentry 2010 This work is

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.