Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sets and Dictionaries Examples Copyright © Software Carpentry 2010

Similar presentations


Presentation on theme: "Sets and Dictionaries Examples Copyright © Software Carpentry 2010"— Presentation transcript:

1 Sets and Dictionaries Examples Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License See for more information.

2 How early was each kind of bird seen?
2

3 How early was each kind of bird seen?
:38 loon :02 goose :07 loon :09 ostrich # hallucinating? :29 loon ⋮ ⋮ ⋮ 3

4 How early was each kind of bird seen?
:38 loon :02 goose :07 loon :09 ostrich # hallucinating? :29 loon ⋮ ⋮ ⋮ Want the minimum of all times associated with a bird 4

5 How early was each kind of bird seen?
:38 loon :02 goose :07 loon :09 ostrich # hallucinating? :29 loon ⋮ ⋮ ⋮ Want the minimum of all times associated with a bird Use bird name as dictionary key 5

6 How early was each kind of bird seen?
:38 loon :02 goose :07 loon :09 ostrich # hallucinating? :29 loon ⋮ ⋮ ⋮ Want the minimum of all times associated with a bird Use bird name as dictionary key And earliest observation time as value 6

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

8 Setup 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 Setup 8

9 Get data from each line of the file 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 Get data from each line of the file 9

10 Check that the data might be right 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 10

11 Store it 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 Store it 11

12 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 12

13 Setup 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 Setup 13

14 Process each tuple in turn 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 Process each tuple in turn 14

15 First sighting, so this must be earliest time
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 First sighting, so this must be earliest time 15

16 Subsequent sighting, so take minimum 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 Subsequent sighting, so take minimum 16

17 What birds were seen on each day?

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

19 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

20 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 20

21 Setup 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 Setup 21

22 Process each tuple in turn 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 Process each tuple in turn 22

23 Make sure there is space to record this bird 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 Make sure there is space to record this bird 23

24 Record this bird 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 Record this bird 24

25

26 :38 loon ' ' 'loon'

27 :38 loon :02 goose ' ' 'goose' 'loon'

28 :38 loon :02 goose :07 loon ' ' 'goose' 'loon'

29 :38 loon :02 goose :07 loon :09 ostrich # hallucinating? ' ' ' ' 'goose' 'ostrich' 'loon'

30 :38 loon :02 goose :07 loon :09 ostrich # hallucinating? :29 loon ' ' ' ' 'loon' 'goose' 'ostrich' 'loon'

31 Which bird did we see least frequently?
31

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

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

34 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 34

35 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 35

36 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 36

37 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 37

38 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 38

39 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: elif counts[bird] == number: result.add(bird) 39

40 Case 1: first bird (initializing data structures)
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: elif counts[bird] == number: result.add(bird) Case 1: first bird (initializing data structures) 40

41 Case 2: new minimum, so replace everything
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: elif counts[bird] == number: result.add(bird) Case 2: new minimum, so replace everything 41

42 Case 3: tied equal for minimum
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: elif counts[bird] == number: result.add(bird) Case 3: tied equal for minimum 42

43 Before the loop { 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number
result Before the loop

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

45 Case 2: new minimum, so replace everything
{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 1 result 'goose' Case 2: new minimum, so replace everything

46 Case 3: tied equal for minimum
{ 'loon' : 3, 'goose' : 1, 'ostrich' : 1 } number 1 'ostrich' result 'goose' Case 3: tied equal for minimum

47 Greg Wilson created by July 2010 Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License See for more information.


Download ppt "Sets and Dictionaries Examples Copyright © Software Carpentry 2010"

Similar presentations


Ads by Google