Data Structures UW CSE 190p Summer 2012. >>> xs = range(3) >>> xs = [1,2,3] >>> xs = [‘a’,’b’,’c’] >>> xs = [1, ‘a’, 3] >>> xs = [[1,2,3], [‘a’,’b’,’c’]]

Slides:



Advertisements
Similar presentations
List comprehensions UW CSE 140 Winter Ways to express a list 1.Explicitly write the whole thing: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81,
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Container Types in Python
Dictionaries: Keeping track of pairs
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Python Control of Flow.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
General Programming Introduction to Computing Science and Programming I.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
More on Functions; Some File I/O UW CSE 190p Summer 2012.
Built-in Data Structures in Python An Introduction.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Python Functions.
Design Exercise UW CSE 190p Summer 2012 download examples from the calendar.
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.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Dictionaries Ruth Anderson CSE 160 University of Washington 1.
Interpreting Exceptions UW CSE 160 Spring 2015 download examples from the calendar 1.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Dictionaries Ruth Anderson UW CSE 160 Winter
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Scientific Programming in Python -- Cheat Sheet
Introduction to Computing Science and Programming I
Sharing, mutability, and immutability
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Data Structures: Lists
Ruth Anderson University of Washington CSE 160 Spring 2015
List comprehensions (and other shortcuts) UW CSE 160 Winter 2016.
Announcements Project 4 due Wed., Nov 7
Tutorial Lecture for EE562 Artificial Intelligence for Engineers
List comprehensions (and other shortcuts) UW CSE 160 Winter 2017.
The Python Data Model UW CSE 190p Summer 2012.
Design Exercise UW CSE 140 Winter 2014.
Design Exercise UW CSE 160 Winter 2017.
Sharing, mutability, and immutability
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson University of Washington CSE 160 Spring 2018
Sharing, mutability, and immutability
Ruth Anderson University of Washington CSE 160 Winter 2017
Sharing, mutability, and immutability
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
Ruth Anderson UW CSE 140 Winter 2014
Sharing, mutability, and immutability
Introduction to Dictionaries
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Michael Ernst UW CSE 140 Winter 2013
Sharing, mutability, and immutability
Michael Ernst CSE 140 University of Washington
(more) Python.
Dictionaries: Keeping track of pairs
For loops Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 2 due today.
“Everything Else”.
Nate Brunelle Today: Style, Collections
Presentation transcript:

Data Structures UW CSE 190p Summer 2012

>>> xs = range(3) >>> xs = [1,2,3] >>> xs = [‘a’,’b’,’c’] >>> xs = [1, ‘a’, 3] >>> xs = [[1,2,3], [‘a’,’b’,’c’]] # list of lists? >>> xs = [x * 2 for x in range(3)] >>> xs = [cels_to_faren(temp) for temp in measurements] >>> warmdays = [temp for temp in msrmts if temp > 20]

List “Comprehensions” ctemps = [17.1, 22.3, 18.4, 19.1] ftemps = [] for c in ctemps: f = celsius_to_farenheit(c) ftemps.append(f) ftemps = [celsius_to_farenehit(c) for c in ctemps] Compare these two snippets for converting a list of values from celsius to farenheit: This syntax is VERY useful: It is usually shorter, more readable, and more efficient. Let it become second nature!

From Last Class def count_unique(doc): “““return the number of unique words in a list of strings””” unique_count = 0 scratchpad = [] for word in doc: if not word in scratchpad: unique_count = unique_count + 1 return unique_count We wrote this function:

New Problem: Top 10 Most Common Words

Exercise (5 min) def top10(doc): “““return a list of the top 10 most frequent words””” # initialize a histogram for word in doc: # if word in histogram # increment its count # else # add it to the histogram # search histogram for top 10 # most frequent words # return result Sketch a function to return the top 10 most common words in a document What data structure(s) will you use?

First Attempt: Two lists def top10(doc): “““return a list of the top 10 most frequent words””” uniquewords = [] counts = [] for word in doc: if word in uniquewords: position = uniquewords.index(word) counts[position] = counts[position] + 1 else: uniquewords.append(word) # now search for top 10 most frequent words… uniquewords = [‘lol’, ‘omg’, ‘know’] counts = [45, 23, 12]

A list of (count, word) pairs def top10(doc): “““return a list of the top 10 most frequent words””” uniquewords_with_counts = [] for word in doc: match = [pair for pair in uniquewords if pair[1] == word] if match != []: pair = match[0] # why is this line here? pair[0] = pair[0] + 1 else: uniquewords.append([1, word]) # now search for top 10 most frequent words uniquewords.sort() return uniquewords[0:10] >>> uniquewords = [ [45, ‘lol’], [23, ‘omg’], [12, ‘know’] ] >>> uniquewords[1][0]

Digression: Lexicographic Order Aaron Andrew Angie a aaa aaaaa [1, 9, 9] [2, 1] [3] [1] [1,1] [1,1,1]

Mapping Values to Values >>> somelist = [‘a’,’b’,’c’] >>> somelist[1] ‘b’ A list can be thought of as a (partial) function from integers to list elements. What if we want to map strings to integers? Or strings to strings? We can say this list “maps integers to strings”

Python Dictionaries >>> phonebook = dict() >>> phonebook[“Alice”] = “ ” >>> phonebook[“Bob”] = “ ” >>> atomicnumber = {} >>> atomicnumber[“H”] = 1 >>> atomicnumber[“Fe”] = 26 >>> atomicnumber[“Au”] = 79 >>> state = {“Atlanta” : “GA”, “Seattle” : “WA”}

Python Dictionaries (2) >>> atomicnumber = {} >>> atomicnumber[“H”] = 1 >>> atomicnumber[“Fe”] = 26 >>> atomicnumber[“Au”] = 79 >>> atomicnumber.keys() ['H', 'Au', 'Fe'] >>> atomicnumber.values() [1, 79, 26] >>> atomicnumber.items() [('H', 1), ('Au', 79), ('Fe', 26)] >>> atomicnumber["Au"] 79 >>> atomicnumber["B”] Traceback (most recent call last): File " ", line 1, in atomicnumber["B"] KeyError: 'B' >>> atomicnumber.has_key("B") False

Top k with a dictionary def top10(doc): “““return a list of the top 10 most frequent words””” uniquewords = {} for word in doc: if uniquewords.has_key(‘omg’): uniquewords[‘omg’] = uniquewords[‘omg’] + 1 else: uniquewords[‘omg’] = 1 # now search for top 10 most frequent words bycount = [(pair[1], pair[0]) for pair in uniquewords.items()] bycount.sort() return bycount[0:10] >>> uniquewords = { ‘lol’:45, ‘omg’:23, ‘know’:12 } >>> uniquewords[‘omg’] This “default” pattern is so common, there is a special method for it.

Top k with a dictionary def top10(doc): “““return a list of the top 10 most frequent words””” uniquewords = {} for word in doc: uniquewords[‘omg’] = uniquewords.set_default(‘omg’, 0) + 1 # now search for top 10 most frequent words bycount = [(pair[1], pair[0]) for pair in uniquewords.items()] bycount.sort() return bycount[0:10] >>> uniquewords = { ‘lol’:45, ‘omg’:23, ‘know’:12 } >>> uniquewords[‘omg’]

Types: some definitions and context Some historical languages were untyped – You could, say, divide a string by a number, and the program would continue. – The result was still nonsense, of course, and program behavior was completely undefined. – This was considered unacceptable Modern languages may be staticly typed or dynamically typed – “staticly typed” means that types are assigned before the program is executed – “dynamically typed” means that types are assigned (and type errors caught) at runtime Modern languages may be strongly typed or weakly typed – For our purposes, “weakly typed” means the language supports a significant number of implicit type conversions. For example, (5 + “3”) could trigger a conversion from “3” to 3 For our purposes, Python can be considered – strongly typed – dynamically typed

Guess the Types def mbar_to_mmHg(pressure): return pressure *

Guess the Types def abs(x): if val < 0: return -1 * val else: return 1 * val

Guess the Types def debug(x): print x

Guess the Type def index(value, some_list): i = 0 for c in somelist: if c == value: return i i = i + 1

Duck Typing “If it walks like a duck and it talks like a duck, then it must be a duck.” (Note: this analogy can be misleading!) At runtime, the operands are checked to make sure they support the requested operation. >>> 3 + “3” >>> for i in 5:... print i

Takeaway Think about types when designing functions, when debugging, when reading code, when writing code….all the time. Ask yourself “What is this variable allowed to be?” – A list, or anything compatible with a for loop? – An integer, or anything that can be multiplied by an integer?

Mutable and Immutable Types >>> def increment(uniquewords, word):... “““increment the count for word”””... uniquewords[word] = uniquewords.setdefault(word, 1) + 1 >>> mywords = dict() >>> increment(mywords, “school”) >>> print mywords {'school': 2} >>> def increment(value):... “““increment the value???”””... value = value + 1 >>> myval = 5 >>> increment(myval) >>> print myval 5

Tuples and Lists def updaterecord(record, position, value): “““change the value at the given position””” record[position] = value mylist = [1,2,3] mytuple = (1,2,3) updaterecord(mylist, 1, 10) updaterecord(mytuple, 1, 10) print mylist print mytuple

Why did they do this? >>> citytuple = (“Atlanta”, “GA”) >>> type(citytuple) >>> citylist = [“Atlanta”, “GA”] >>> weather[citytuple] = “super hot” >>> weather[citylist] = “super hot” Traceback (most recent call last): File " ", line 1, in TypeError: unhashable type: 'list' Answer: Performance. If the system knows for sure that a value can never be changed, it can cheat.

No really, why? >>> citylist = [“Atlanta”, “GA”] >>> weather[citylist] = “super hot” >>> citylist[1] = “Georgia” >>> weather[[“Atlanta”, “GA”]] ???

Mutable and Immutable Types Immutable – numbers, strings, tuples Mutable – lists and dictionaries