Introduction to Python Dictionaries

Similar documents
Lab 10: Color Sort Turtles not yet sorted by color

1 Turtle Graphics Concepts

SEVENTH'ANNUAL'JUILFS'CONTEST' SPRING'2015' ' '

In this project you will use loops to create a racing turtle game and draw a race track.

Turtle Ballet: Simulating Parallel Turtles in a Nonparallel LOGO Version. Erich Neuwirth

COMP Intro to Logic for Computer Scientists. Lecture 9

1 #L V, Beginner Books A? 1

ENGL-3 MMS Running on Water Quiz Exam not valid for Paper Pencil Test Sessions

CS108L Computer Science for All Module 7: Algorithms

Dasher Web Service USER/DEVELOPER DOCUMENTATION June 2010 Version 1.1

Egg laying vs. Live Birth

Subdomain Entry Vocabulary Modules Evaluation

Biology 164 Laboratory

2010 Canadian Computing Competition Day 1, Question 1 Barking Dogs!

Machine Learning.! A completely different way to have an. agent acquire the appropriate abilities to solve a particular goal is via machine learning.

Dairy Industry Network Data Standards. Animal Life Data. Discussion Document

Maps Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Multiclass and Multi-label Classification

Lacey Blocker Vernon Parish Teacher Leader NBCT

News English.com Ready-to-use ESL / EFL Lessons

Grandparents U, 2018 Part 2

Finch Robot: snap level 4

Python 3 Turtle graphics. Lecture 24 COMPSCI111/111G SS 2017

Teaching notes and key

Geometry from Scratch

Lab 6: Energizer Turtles

CSSE 374 Software Architecture and Design I

StarLogo Complete Command List (Edited and reformatted by Nicholas Gessler, 6 June 2001.)

5 State of the Turtles

Lecture 1: Turtle Graphics. the turtle and the crane and the swallow observe the time of their coming; Jeremiah 8:7

Sketch Out the Design

Lab 5: Bumper Turtles

Shell (cont d) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Recurrent neural network grammars. Slide credits: Chris Dyer, Adhiguna Kuncoro

Hello Scratch! by Gabriel Ford, Sadie Ford, and Melissa Ford. Sample Chapter 3. Copyright 2018 Manning Publications

Singular or Plural Sort

News English.com Ready-to-use ESL / EFL Lessons

The World's Best Jumper

A Teacher s Guide to Fur, Feathers, and Scales Grades PreK 2

Moving towards formalisation COMP62342

Design of 32 bit Parallel Prefix Adders

DESIGN AND SIMULATION OF 4-BIT ADDERS USING LT-SPICE

READING: Scientists are Making Dinosaurs!

The role of diagnosticians in terrestrial animal disease surveillance CAHLN presentation, May 2013

Getting Started with Java Using Alice. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

MOON PHASES FOR 2018, at Kitt Peak Times and dates are given in local time, zone = 7 hr West. They are generally better than +- 2 minutes.

MOON PHASES FOR 2019, at Kitt Peak Times and dates are given in local time, zone = 7 hr West. They are generally better than +- 2 minutes.

A Peek Into the World of Streaming

LISTEN A MINUTE.com. Dogs. One minute a day is all you need to improve your listening skills.

DOWNLOAD OR READ : CAT 2002 SOLUTIONS PDF EBOOK EPUB MOBI Page 1

Okapi: Half Giraffe, Half Zerba By Mikki Sadil

Scratch Lesson Plan. Part One: Structure. Part Two: Movement

Package TurtleGraphics

An Esterel Virtual Machine (EVM) Aruchunan Vaseekaran

Okapi: Half Giraffe, Half Zebra By Mikki Sadil

Let s Play Poker: Effort and Software Security Risk Estimation in Software. Picture from

A Teacher s Guide to Unearthing the Past Grades Pre-K 2

Multilevel Script. Teacher s Guide. Animals, Animals. Level E Level H Level K. Levels: E, H, and K Word Count: 460. Story Summary: Cast of Characters:

Pigeonhole Principle

Package PetfindeR. R topics documented: May 22, Type Package Title 'Petfinder' API Wrapper Version Author Aaron Schlegel

Pre-lab Homework Lab 8: Natural Selection

Go, Dog. Go! PLAYGUIDE. The Story Dogs, dogs, everywhere! Big ones, little ones, at work and at play. The CATCO

CAT IN THE HAT TEXT PDF

TE 408: Three-day Lesson Plan

NEWS ENGLISH LESSONS.com

Prof Michael O Neill Introduction to Evolutionary Computation

Microsoft Dexterity. Comprehensive Index Release 12

American Stories To Build a Fire by Jack London. Lesson Plan by Jill Robbins, Ph.D.

DOWNLOAD CATS IN HATS 2007 CALENDAR

Photocopiable Resources

Homework: 1. Catalyst 2. Cladogram Building 3. Jigsaw Reading. Agenda:

Semantics. These slides were produced by Hadas Kotek.

LEARNING OBJECTIVES. Watch and understand a video about a wildlife organization. Watch and listen

SATS. An Explanation of Working Trials Exercises. Plus how to get started/ What to expect for Newcomers to the sport of Working Trials

Public Key Directory: What is the PKD and How to Make Best Use of It

Eggology (Grades K-2)

Dont Let The Pigeon Stay Up Late

Possible Criterion Student Score Score Score TEST RECORD FORM. Vocabulary: Target Vocabulary, Alphabetical Order 10 8

4-H Dog Showmanship. Class: Junior Intermediate Senior. 4-Her s Name Dog s Name Breed Show Location Date Judge. Smiling Friendly Confident.

Writing Simple Procedures Drawing a Pentagon Copying a Procedure Commanding PenUp and PenDown Drawing a Broken Line...

Grade 5, Prompt for Opinion Writing Common Core Standard W.CCR.1

Created by Annette Breedlove In All You Do

The PVS Tool. Part 4. Introduction to the concept of Fundamental Components and Critical Competencies

KB Record Errors Report

CHAPTER 3 MUTATION AND ADAPTIVE TRAITS

THE ARTICLE. New mammal species found

OBJECTIVE: Students work as a class to graph, and make predictions using chicken weight data.

G oing. Milwaukee Youth Arts Center

CIMTRADZ. Capacity building in Integrated Management of Trans-boundary Animal Diseases and Zoonoses

6.14(a) - How to Run CAT Reports Record Errors Report

Read each question carefully

Clean Air. Ann is sick. But I have a pal who may know. She. is a fine doctor and I think you need to go see

The Signet Guide to.. providing electronic sheep data

Massachusetts State Search & Rescue Dog Federation Basic Human Remains Detection Canine Evaluation Form

VA4PR.1. Create artworks based on personal experience and selected themes.

A SPATIAL ANALYSIS OF SEA TURTLE AND HUMAN INTERACTION IN KAHALU U BAY, HI. By Nathan D. Stewart


LISTEN A MINUTE.com. Chickens. Focus on new words, grammar and pronunciation in this short text.

MACHINE PROBLEM 8 -"IN THE MIDDLE, A CAULDRON BOILING" CS xxx - Fall Quarter yyyy - Dr. Estell - DUE AT CLASSTIME mm/dd/yyyy

Transcription:

Introduction to Python Dictionaries Mar 10, 2016 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 1

ACT2-4 Let s talk about Task 2 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 2

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Get the vocabulary size of Moby Dick (Attempt 1) Write test cases to make sure our program works Think of a faster way to compute the vocabulary size Save ACT2-4.py and MobyDick.txt to the same directory CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 3

Writing a vocabsize Function def vocabsize(): mylist = readmobydickshort() uniquelist = noreplicates(mylist) return len(uniquelist) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 4

Writing a vocabsize Function def vocabsize(): mylist = readmobydickshort() uniquelist = noreplicates(mylist) return len(uniquelist) def noreplicates(wordlist): '''takes a list as argument, returns a list free of replicate items. slow implementation.''' def iselementof(myelement,mylist): '''takes a string and a list and returns True if the string is in the list and False otherwise.''' CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 5

Writing a vocabsize Function def vocabsize(): mylist = readmobydickshort() uniquelist = noreplicates(mylist) return len(uniquelist) def noreplicates(wordlist): '''takes a list as argument, returns a list free of replicate items. slow implementation.''' def iselementof(myelement,mylist): '''takes a string and a list and returns True if the string is in the list and False otherwise.''' def testnoreplicates(): def testiselementof(): Writing test cases is important to make sure your program works! CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 6

Slow Implementation def iselementof(myelement,mylist): '''Takes a string and a list and returns True if the string is in the list and False otherwise.''' for e in mylist: if e == myelement: return True return False def noreplicates(wordlist): '''Takes a list as argument, returns list free of replicates''' newlist = [] for w in wordlist: if iselementof(w, newlist) == False: newlist = newlist + [w] return newlist Slow! Many list traversals! CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 7

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Get the vocabulary size of Moby Dick (Attempt 1) Write test cases to make sure our program works Think of a faster way to compute the vocabulary size CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 8

What does slow implementation mean? Replace readmobydickshort() with readmobydickall() Now, run vocabsize() Hint: Ctrl-C (or Command-C) will abort the call. CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 9

What does slow implementation mean? Replace readmobydickshort() with readmobydickall() Now, run vocabsize() Hint: Ctrl-C (or Command-C) will abort the call. Faster way to write noreplicates() What if we can sort the list? [ a, a, a, at, and, and,, zebra ] CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 10

Sorting Lists Preloaded Functions Name Inputs Outputs CHANGES sort List Original List! CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 11

Sorting Lists Preloaded Functions Name Inputs Outputs CHANGES sort List Original List! >>> mylist = [0,4,1,5,-1,6] >>> mylist.sort() >>> mylist [-1, 0, 1, 4, 5, 6] CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 12

Sorting Lists Preloaded Functions Name Inputs Outputs CHANGES sort List Original List! >>> mylist = [0,4,1,5,-1,6] >>> mylist.sort() >>> mylist [-1, 0, 1, 4, 5, 6] >>> mylist = ['b','d','c','a','z','i'] >>> mylist.sort() >>> mylist ['a', 'b', 'c', 'd', 'i', 'z'] CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 13

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Summary Statistics Get the vocabulary size of Moby Dick (Attempt 1) Write test cases to make sure our program works Think of a faster way to compute the vocabulary size CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 14

Homework Sort your original list Make a new list, with initially only the first element of the original list For each element in the original list (from the second element on): If that element is not the same as the previous Add to the new list Much faster! Only one list traversal! CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 15

Remember what we re doing Doing text analysis Introducing you to computer programming In Python! and we are introducing these concepts swiftly Takes practice! Questions / office hours meetings are expected We re here to help CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 16

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Get Word Frequencies Define the inputs and the outputs Learn a new data structure Write a function to get word frequencies Go from word frequencies to a concordance (finally!) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 17

Word Frequency: Inputs and Outputs The cat had a hat. The cat sat on the hat. I want to write a wordfreq function CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 18

Word Frequency: Inputs and Outputs The cat had a hat. The cat sat on the hat. I want to write a wordfreq function What is the input to wordfreq? CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 19

Word Frequency: Inputs and Outputs The cat had a hat. The cat sat on the hat. I want to write a wordfreq function What is the input to wordfreq? What is the output of wordfreq? CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 20

Word Frequency: Inputs and Outputs The cat had a hat. The cat sat on the hat. I want to write a wordfreq function What is the input to wordfreq? What is the output of wordfreq? Word Freq. the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 21

Word Frequency: Inputs and Outputs The cat had a hat. The cat sat on the hat. I want to write a wordfreq function What is the input to wordfreq? What is the output of wordfreq? We could do this with a list. How? Word Freq. the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 22

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Get Word Frequencies Define the inputs and the outputs Learn a new data structure Write a function to get word frequencies Go from word frequencies to a concordance (finally!) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 23

A New Data Structure A Data Structure is simply a way to store information. Lists are a type of data structure We can have lists of integers, floats, strings, booleans, or any combination. Organized linearly (indexed by a range of integers) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 24

A New Data Structure The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 25

A New Data Structure The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 Associate each word with the frequency. CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 26

A New Data Structure The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 Associate each word with the frequency. Keys Values CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 27

A New Data Structure The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 Associate each word with the frequency. Keys Values Key-Value Pairs Key Value the 3 cat 2 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 28

A New Data Structure: Dictionaries Keys can be almost any type or data structure. Values can be any type or data structure. Key Type Value Type Example Key Example Value CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 29

A New Data Structure: Dictionaries Keys can be almost any type or data structure. Values can be any type or data structure. Key Type Value Type Example Key Example Value String Integer 'the' 3 String Integer 'cat' 2 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 30

A New Data Structure: Dictionaries Keys can be almost any type or data structure. Values can be any type or data structure. Key Type Value Type Example Key Example Value String Integer 'the' 3 String Integer 'cat' 2 String String 'Geisel' '078-05-1120' String String 'Whitcher' '552-38-5014' CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 31

A New Data Structure: Dictionaries Keys can be almost any type or data structure. Values can be any type or data structure. Key Type Value Type Example Key Example Value String Integer 'the' 3 String Integer 'cat' 2 String String 'Geisel' '078-05-1120' String String 'Whitcher' '552-38-5014' Float String 1.0 'one point oh' Float String 2.8 'two point eight' CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 32

A New Data Structure: Dictionaries Keys can be almost any type or data structure. Values can be any type or data structure. Key Type Value Type Example Key Example Value String Integer 'the' 3 String Integer 'cat' 2 String String 'Geisel' '078-05-1120' String String 'Whitcher' '552-38-5014' Float String 1.0 'one point oh' Float String 2.8 'two point eight' Integer List 1638 [2, 3, 3, 7, 13] CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 33

A New Data Structure: Dictionaries The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 >>> freq = {} >>> freq {} >>> Initialize a Dictionary CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 34

A New Data Structure: Dictionaries The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 >>> freq = {} >>> freq {} >>> freq['the'] = 3 >>> freq {'the': 3} >>> Initialize a Dictionary Key = the Value = 3 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 35

A New Data Structure: Dictionaries The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 >>> freq = {} >>> freq {} >>> freq['the'] = 3 >>> freq {'the': 3} >>> freq['cat'] = 2 >>> freq {'the': 3, 'cat': 2} >>> Initialize a Dictionary Key = the Value = 3 Key = cat Value = 2 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 36

A New Data Structure: Dictionaries The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 >>> freq2 = {'the':3,'cat':2} >>> freq2 {'the': 3, 'cat': 2} >>> Initialize a dictionary with two key-value pairs CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 37

A New Data Structure: Dictionaries The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 >>> freq2 = {'the':3,'cat':2} >>> freq2 {'the': 3, 'cat': 2} >>> >>> freq2['cat'] 2 >>> freq2['the'] 3 Retrieve a value using the key CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 38

Redefining things in the dictionary The cat had a hat. The cat sat on the hat. Word Frequency the 3 cat 27 had 1 a 12 hat 2 sat 1 on 1 >>> freq2 = {'the':3,'cat':2} >>> freq2 {'the': 3, 'cat': 2} >>> >>> freq2['cat'] = 7 >>> freq2['a'] = freq2['a']+1 Assign a new value to a key! CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 39

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Get Word Frequencies Define the inputs and the outputs Learn a new data structure Write a function to get word frequencies Go from word frequencies to a concordance (finally!) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 40

Python Dictionaries Function (All operate on Dictionaries) Input Output Example keys() None List of keys >>> freq2.keys() ['cat, 'the ] Keys Are Unique! Assigning/getting any value is very fast CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 41

Python Dictionaries Function (All operate on Dictionaries) Input Output Example keys() None List of keys values() None List of values >>> freq2.keys() ['cat, 'the'] >>> freq2.values() [2, 3] Keys Are Unique! Assigning/getting any value is very fast CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 42

Python Dictionaries Function (All operate on Dictionaries) Input Output Example keys() None List of keys values() None List of values >>> freq2.keys() ['the', 'cat'] >>> freq2.values() [3, 2] <key> in <dict> Key Boolean >>> zebra in freq2 False <key> in <dict> (same as above) >>> cat' in freq2 True Keys Are Unique! Assigning/getting any value is very fast CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 43

Python Dictionaries Function (All operate on Dictionaries) Input Output Example keys() None List of keys values() None List of values >>> freq2.keys() ['the', 'cat'] >>> freq2.values() [3, 2] <key> in <dict> Key Boolean >>> zebra in freq2 False <key> in <dict> (same as above) >>> cat' in freq2 True del(<dict>[<key>]) Dict. Entry None Keys Are Unique! Assigning/getting any value is very fast >>> del(freq2['cat']) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 44

Python Dictionaries Function (All operate on Dictionaries) Input Output Example keys() None List of keys values() None List of values >>> freq2.keys() ['the', 'cat'] >>> freq2.values() [3, 2] <key> in <dict> Key Boolean >>> zebra in freq2 False False <key> in <dict> (same as above) >>> 'the' in freq2 True del(<dict>[<key>]) Dict. Entry None >>> del(freq2['cat']) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 45

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Get Word Frequencies Define the inputs and the outputs Learn a new data structure Write a function to get word frequencies Go from word frequencies to a concordance (finally!) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 46

Python Dictionaries Function (All operate on Dictionaries) Input Output Example keys() None List of keys values() None List of values <key> in <dict> Key True or False <key> in <dict> del(<dict>[<key>]) (means same as above) Dict. Entry None >>> freq2.keys() ['the', 'cat'] >>> freq2.values() [3, 2] >>> zebra in freq2 False >>> cat' in freq2 True >>> del(freq2[ cat']) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 47

Python Dictionaries The cat had a hat. The cat sat on the hat. I want to write a wordfreq function What is the input to wordfreq? What is the output of wordfreq? Word Freq. the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 48

Python Dictionaries The cat had a hat. The cat sat on the hat. I want to write a wordfreq function What is the input to wordfreq? What is the output of wordfreq? Word Freq. the 3 cat 2 had 1 a 1 hat 2 sat 1 on 1 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 49

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Get Word Frequencies Define the inputs and the outputs Learn a new data structure Write a function to get word frequencies Go from word frequencies to a concordance (finally!) CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 50

Building a Concordance The cat had a hat. The cat sat on the hat. 0 1 2 3 4 5 6 7 8 9 10 Word List of Positions Frequency the [0,5,9] 3 cat [1,6] 2 had [2] 1 a [3] 1 hat [4,10] 2 sat [7] 1 on [8] 1 CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 51

The Big Picture Overall Goal Build a Concordance of a text Locations of words Frequency of words Today: Get Word Frequencies Define the inputs and the outputs Learn a new data structure Write a function to get word frequencies Go from word frequencies to a concordance (finally!) This will be part of your next HW CSCI 0931 - Intro. to Comp. for the Humanities and Social Sciences 52