Week 5: Dictionaries and tuples

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
“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?
An Introduction to Textual Programming
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Built-in Data Structures in Python An Introduction.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Tuples Chapter 10 Python for Informatics: Exploring Information
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
CS100 - PYTHON – EXAM 2 REVIEW -ONLY THE VITAL STUFF- PYTHON STRING METHODS, LOOPS, FILES, AND DICTIONARIES.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Intro to CS Nov 21, 2016.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Week 1: Writing your first program
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Tuples and Lists.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
When to use Tuples instead of Lists
Chapter 4 Strings & Tuples
CSc 120 Introduction to Computer Programing II
Python Lists Chapter 8 Python for Everybody
Department of Computer Science,
Week 3: Loops and strings
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Announcements Project 4 due Wed., Nov 7
Week 4: Files and lists Python workshop Week 4: Files and lists
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
CompSci 101 Introduction to Computer Science
Week 3: Loops and strings
Files and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to PHP & Variables
Tuples Chapter 10 Python for Everybody
Engineering Innovation Center
CISC101 Reminders Quiz 2 this week.
Learning to Program in Python
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
I210 review.
Python for Informatics: Exploring Information
Introduction to Dictionaries
Python programming exercise
Python Lists and Sequences
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 6: Control Flow Tools (for and while loops)
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Python Review
“Everything Else”.
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Starter Activities GCSE Python.
Python - Tuples.
Introduction to PYTHON
Tuple.
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Week 5: Dictionaries and tuples barbera@van-schaik.org Python workshop Week 5: Dictionaries and tuples barbera@van-schaik.org

Overview of this workshop series Week 1: Writing your first program Week 2: Make choices and reuse code Week 3: Loops and strings Week 4: Files and lists Week 5: Dictionaries and tuples Acknowledgments: Structure of the workshop follows the book “Python for informatics” by Charles Severance. Several examples are from this book or the accompanying slides.

Last weeks exercises

Assignment 1 Image: Larson How many lines with the word “true” and how many with “false” in plato.txt? Hints Open file Make two variables to count “true” and “false” Use the string method: find Image: Larson assignment1.py

Assignment 2 Open the file “hobbies.txt” and print the names of the persons Hints: Open file “split” the lines Get the right column and print it http://www.verbaarschot-partners.nl/aanvullende-scholing/ assignment2.py

Bonus exercise – guessing game Let the user think of a number between 1 and 1000 The computer makes guesses The user gives hints: higher/lower (or h/l) One solution: let computer guess all the numbers between 1 and 1000 (not so efficient) How would you solve this? With code or as a concept http://vehiclefixer.com/videos/how-to-tell-which-bank-is-bank-1-and-guess-whats-wrong-game/guessing-game/ guessing-game3.py

Guessing game - concept User thinks of number 9 1 2 3 4 5 6 7 8 9 10 higher higher higher YES

Dictionaries

What is a dictionary? Key-value pairs Een → One Twee → Two Drie → Three >>> nl2en = dict() >>> print(nl2en) {} Vandale.nl

Put data in dictionary http://www.tootsie.com/howmanylick-experiments >>> nl2en = dict() >>> nl2en[“een”] = “one” >>> nl2en[“twee”] = “two” >>> nl2en[“drie”] = “three” >>> print(nl2en) >>> altdict = {'een':'one', 'twee':'two', 'drie':'three'} >>> print(altdict) http://www.tootsie.com/howmanylick-experiments Order is not the same as entered Lists are ordered, dictionaries are not

Get data out http://blog.lionbridge.com/translation/ >>> print(nl2en[“twee”]) # shows value >>> print(nl2en[“hallo”]) # does not exist: error >>> for key in nl2en: >>> print(key, nl2en[key]) http://blog.lionbridge.com/translation/

Functions and operations >>> len(nl2en) # get nr of key-value pairs >>> 'een' in nl2en # True >>> 'one' in nl2en # False >>> 'one' in nl2en.values() # True https://docs.python.org/2.7/library/stdtypes.html#typesmapping

Count stuff import sys myfile = "plato.txt" try: fh = open(myfile, "r") except: sys.exit("cannot open file") count-words.py

Count stuff # Go through file, get the words and count the words d = dict() for line in fh: line = line.rstrip() # remove newline line = line.lower() # make everything lowercase words = line.split() # split the line on tab or space for word in words: d[word] = d.get(word, 0) + 1 count-words.py

Count stuff print("Show 10 words and their frequency") i = 0 for key in d: if i >= 10: break print(key, d[key]) i += 1 count-words.py

Count stuff print("Show the TOP 10 words and their frequency") i = 0 for key in sorted(d, key=d.get, reverse=True): if i >= 10: break print(key, d[key]) i += 1 count-words.py

Tuples

What is a tuple? A tuple is like a list, but immutable >>> t = tuple() >>> print(t) () >>> t = 'a', 'b', 'c', 'd', 'e' # tuple is comma separated >>> t = ('a', 'b', 'c', 'd', 'e') # clearer way to do this >>> tup = ('a',) # single value needs to have a comma https://www.youtube.com/watch?v=92osKnSgkWw

Tuple assignments Need to assign all values at once It is possible to replace an entire tuple >>> t = ('a', 'b', 'c', 'd', 'e') >>> t[0] = 'bla' # not possible >>> t.append('bla') # also not possible >>> t = ('bla',) + t # this is possible

Compare tuples >>> (0, 1, 2) < (0, 3, 4) # True >>> (0, 1, 2000000) < (0, 3, 4) # True? >>> (10, 1, 2) < (0, 3, 4) # False It starts at the first elements and stops once it is not True anymore https://en.wikipedia.org/wiki/Elephant https://en.wikipedia.org/wiki/Mouse <

Sort on first element, in case of a tie try second element txt = 'but soft what light in yonder window breaks' words = txt.split() t = list() for word in words: t.append((len(word), word)) t.sort(reverse=True) res = list() for length, word in t: res.append(word) print(res) This is a tuple in a list The comma-separated length, word is a tuple as well Example from book tuple-sort.py

Multiple assignments at once >>> x, y = 'hello', 'there' >>> print(x) >>> print(y) >>> x, y = y, x # swap values >>> mail = 'barbera@van-schaik.org' >>> name,domain = mail.split('@') http://blog.emptylemon.co.uk/2012/03/guide-project-management-methodologies/

Dictionaries and tuples >>> d = { 'socrates': 10, 'gorgias': 5, 'polus': 2} >>> t = d.items() >>> print(t) >>> sorted(t) Multiple assignments in a script for key, value in sorted(d.items()): print(key, value) www.chsbs.cmich.edu

Strings, lists, dictionaries, tuples String: immutable, iterate over characters >>> s = “hooray” List: mutable, ordered, index is a number >>> l = ['aap','noot','mies'] Dictionary: key-value pairs, mutable, unordered >>> d = {'aap':10, 'noot':5, 'mies'=25} Tuple: immutable, ordered, index is a number >>> t = ('a', 'b', 'c', 'd')

Dictionaries and tuples Chapter 9 and 10 contain more comments on these topics... and exercises http://www.pythonlearn.com/book.php

Big data, cloud and the internet of things Yep, all the buzz words

Big data DNA sequencing produces 9 GB data Analyzing this takes several days Want to speed this up Want to track the progress http://www.biol.unt.edu/~jajohnson/DNA_sequencing_process

Computations and monitoring One machine for monitoring Gets status updates from all machines Several machines that do the computations in parallel Each machine reports its status RUNNING/FAILED/FINISHED

Monitoring, technical stuff Machines with job Runs a python CGIHTTPServer set-status.py ip:1.2.3.4 status:RUNNING message:”Doing stuff” Presents status of the running job in json format Monitor List with ip-adresses of machine Python script gets json file from each machine Prints a list with statuses

IoT – monitoring on smartwatch Machine for monitoring Gets status updates from all machines Prints all statuses Presents all statuses in a format my watch understands On the watch An app that “talks” with the monitoring machine Blinks and vibrates when all computations are complete

End of this course https://commons.wikimedia.org/wiki/File:Thats_all_folks.svg