Files and Dictionaries

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
HW 1: Problems 3 & 4 EC 1 & 2.
Introduction to Computing Using Python More Built-in Container Classes  Container Class dict  Encoding of String Characters  Randomness and Random Sampling.
CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Ionut Trestian Northwestern University
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
IS 313 Today Schedule Week 0: files dictionaries You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where we've been… Where.
EECS 110: Lec 13: Dictionaries Aleksandar Kuzmanovic Northwestern University
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
CS100 - PYTHON – EXAM 2 REVIEW -ONLY THE VITAL STUFF- PYTHON STRING METHODS, LOOPS, FILES, AND DICTIONARIES.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
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.
Reading Files >>> f = file( 'a.txt' ) >>> text = f.read() >>> text 'This is a file.\nLine 2\nLast line!\n' >>> f.close() In Python reading files is no.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
GCSE COMPUTER SCIENCE Practical Programming using Python
Scientific Programming in Python -- Cheat Sheet
COMPSCI 107 Computer Science Fundamentals
CS this week 4 Building classes vs. using the library hw10pr2
EECS 110: Lec 13: Dictionaries
Advanced Python Data Structures
When to use Tuples instead of Lists
CSc 120 Introduction to Computer Programing II
loops for loops iterate over a given sequence.
Basic operators - strings
Advanced Python Data Structures
Announcements Project 4 due Wed., Nov 7
Python - Dictionaries.
Week 5: Dictionaries and tuples
Markov Model a language processing model
Files and Dictionaries
String Manipulation.
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Introduction to Python
And now for something completely different . . .
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Introduction to Python
CSc 110, Spring 2018 Lecture 33: Dictionaries
CHAPTER THREE Sequences.
Files and Dictionaries
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Ionut Trestian Northwestern University

CEV208 Computer Programming
Introduction to Dictionaries
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Input and Output with FILES
CS this week 4 Building classes vs. using the library hw10pr2
Nate Brunelle Today: Dictionaries
(more) Python.
Northwestern University
Dictionaries: Keeping track of pairs
Advanced Python Data Structures
15-110: Principles of Computing
CHAPTER 4: Lists, Tuples and Dictionaries
Python Review
Nate Brunelle Today: Dictionaries
CSE 231 Lab 8.
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Introduction to Computer Science
Presentation transcript:

Files and Dictionaries

Files >>> f = open( ‘spam.txt‘, ‘r’ ) In Python reading files is no problem… >>> f = open( ‘spam.txt‘, ‘r’ ) >>> text = f.read() alternative… >>> text = f.read(50) >>> f.close() >>> text 'I like poptarts and 42 and spam.\nWill I ... >>> word_list = text.split() [ 'I', 'like', 'poptarts', ... ] opens the file for reading and calls it f reads the whole file and calls it f reads the next 50 bytes from the file Point out that open is a method on file objects (technically, TextIOStream objects, but stay away from that complexity). And split is a method on string objects. Might want to demonstrate this on the spam.txt file. closes the file (optional) all the text (as one big string) text.split() returns a list of each "word"

Try file_ops.py >>> f = open( ‘greeting.txt‘, ‘w’ ) >>> text = “Hello, world!” >>> f.write(text) >>> f.close() opens the file for writing and calls it f Writes string text to f Try file_ops.py

Count how many words are in a given file def wc( filename ): """ word-counting program """ f = open( filename, “r”) text = f.read() f.close() words = text.split() print("There are", len(words), "words.") file handling word counting This is in cha6_2.py, but it is the complete function, so you might want to go to the next slide first Try wc.py

Dictionaries In Python a dictionary is a set of key - value pairs. 1999 Feb 16 2000 Feb 4 Rabbit 1998 Jan 28 1999 Feb 15 Tiger 1997 Feb 7 1998 Jan 27 Ox 1996 Feb 19 1997 Feb 6 Rat Dictionaries In Python a dictionary is a set of key - value pairs. >>> d = {} >>> d[1996] = ‘Rat' >>> d[1997] = ‘Ox' >>> d {1996:‘Rat', 1997:‘Ox'} >>> d[1996] ‘Rat' >>> d[1969] key error creates an empty dictionary, d 1996 is the key ‘Rat' is the value 1997 is the key ‘Ox' is the value Curly! And colony! Retrieve data like lists… It's a sequence where the index can be any immutable key.

More on dictionaries Dictionaries have lots of built-in methods, or functions: >>> d = {1996:‘Rat', 1997:‘Ox'} >>> 1996 in d >>> 1969 in d True False >>> len(d) 2 >>> list(d.keys()) [ 1996, 1997 ] >>> list(d.values()) [ ‘Rat', ‘Ox' ] >>> list(d.items()) [ (1996, ‘Rat'), (1997, ‘Ox') ] in checks if a key is present len returns the # of keys d.keys returns a list of all keys d.values returns a list of all keys d.items returns a list of all key, value pairs

Try word_freq_count.py and dict_wc.py What if we wanted the number of different words in the file? This would be the author's vocabulary size, instead of the total word count. def wc( filename ): """ word-counting program """ f = open( filename ) text = f.read() f.close() words = text.split() print("There are", len(words), "words.") d = {} for w in words: if w not in d: d[w] = 1 else: d[w] += 1 print ("There are", len(d), "distinct words.\n“) return len(d) # return the number of distict words file handling word counting Tracking the number of occurences of each word with a dictionary, d. Demo wc() using spam.txt. Try word_freq_count.py and dict_wc.py

Vocabularists? Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works) http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html Active vocabulary estimates range from 10,000-60,000. Passive vocabulary estimates are much higher. Many Shakespearean contributions: One contemporary author in the Oxford Eng. Dictionary… Shakespeare which word?

Vocabularists? Shakespeare used 31,534 different words and an estimated vocabulary of 66,534 words (across his works) http://kottke.org/10/04/how-many-words-did-shakespeare-know Average English speaker knows 10,000 to 20,000 words. Many Shakespearean contributions: Shakespeare J. K. Rowling