Sets and Dictionaries Examples Copyright © Software Carpentry 2010

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Algorithms for SELECT and JOIN Operations (8) Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL.
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.
Unit Testing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
16.5 Introduction to Cost- based plan selection Amith KC Student Id: 109.
The Experimental Method in Psychology Explaining Behaviour
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
Recommendations Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Mechanics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Steganography Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Data Abstraction UW CSE 190p Summer Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these.
Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Mark Dunlop, Computer and Information Sciences, Strathclyde University 1 Algorithms & Complexity 5 Games Mark D Dunlop.
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Phylogenetic Trees Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advanced Shell Tricks Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
…and how to do stuff with them Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Design Exercise UW CSE 160 Spring Exercise Given a problem description, design a module to solve the problem 1) Specify a set of functions – For.
Text Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Querying Directory Contents Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See
Operators Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 12 Query Processing (2) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Aliasing Copyright © Software Carpentry 2010
Storage Access Paging Buffer Replacement Page Replacement
Module 11: File Structure
Program Design Invasion Percolation: Aliasing
Debugging Intermittent Issues
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
Database Management System
Program Design Invasion Percolation: Testing
Database Applications (15-415) DBMS Internals- Part VII Lecture 16, October 25, 2016 Mohammad Hammoud.
Debugging Intermittent Issues
Program Design Invasion Percolation: Resolving Ties
Program Design Invasion Percolation: Neighbors
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Data File Import / Export
Here is a puzzle I found on a t-shirt
Design Exercise UW CSE 140 Winter 2014.
Writing Functions( ) (Part 5)
Version Control Basic Operation Copyright © Software Carpentry 2010
Lecture 2- Query Processing (continued)
One-Pass Algorithms for Database Operations (15.2)
Version Control Introduction Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Bugs
Akhila Kondai Exam-2 overview Akhila Kondai
Version Control Conflict Copyright © Software Carpentry 2010
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each.
Program Design Invasion Percolation: The Grid
Adapted from slides by Marty Stepp and Stuart Reges
Browsing Directories Using walk
Presentation transcript:

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

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

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

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

How early was each kind of bird seen? 2010-07-03 05:38 loon 2010-07-03 06:02 goose 2010-07-03 06:07 loon 2010-07-04 05:09 ostrich # hallucinating? 2010-07-04 05: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

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

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

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

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

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

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

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

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

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

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

What birds were seen on each day?

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

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

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

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

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

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

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

2010-07-03 05:38 loon '2010-07-03' 'loon'

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

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

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

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

Which bird did we see least frequently? 31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Greg Wilson created by 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.