More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

Similar documents
File I/O in Python CS 8: Introduction to Computer Science Lecture #11

More Examples Using Lists Tuples and Dictionaries in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #11

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Welcome to Solving Problems with Computers I

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Starting chapter 5. l First open file, and say purpose read or write inputfile = open('mydata.txt', 'r') outputfile = open('myresults.

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

Unit 10: Data Structures CS 101, Fall 2018

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Welcome to Computer Organization and Design Logic

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Pre- Defined Func-ons in C++ Review for Midterm #1

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

CS 150 Introduction to Computer Science 1

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

61A Lecture 7. Monday, September 15

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

61A Lecture 21. Friday, March 13

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

Lecture #21: Search and Sets. Last modified: Wed Mar 9 15:44: CS61A: Lecture #21 1

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

Central Washington University Department of Computer Science Course Syllabus

61A Lecture 4. Monday, September 9

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

ENGR/CS 101 CS Session Lecture 9

CIS192 Python Programming. Robert Rand. August 27, 2015

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

ENGR/CS 101 CS Session Lecture 12

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

CS Introduction to Programming Fall 2016

MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

Dynamic Arrays Makefiles and Mul3ple File Compiles

CS 106B, Lecture 1 Introduction to C++

CS3: Introduction to Symbolic Programming. Lecture 5:

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

[301] JSON. Tyler Caraza-Harter

CS1150 Principles of Computer Science Boolean, Selection Statements

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

Python for Non-programmers

Descriptive Statistics and Graphing

CS 100: Computability, Python Lists

Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th.

Introduction to Python

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS1 Lecture 2 Jan. 16, 2019

Variable and Data Type 2

ME 142 Engineering Computation I. Graphing with Excel

61A Lecture 7. Monday, September 16

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Expressions and Variables

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

61A Lecture 3. Friday, September 5

CS 4800: Algorithms & Data. Lecture 1 January 10, 2017

Transcription:

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10 Ziad Matni Dept. of Computer Science, UCSB

Administrative Tutoring/Review Session Available! TWO of them again! Friday, 5/12 at 1:00 PM & 2:30 PM in PHELPS 3525 Please note: T.A. Sourav s office hours are now Thursday: 10AM to 12PM Midterm #2 is next week on Thursday 5/18! 5/9/17 Matni, CS16, Sp17 2

REVIEW Lists Use square brackets, [ ] to define a list fruit = [ apple, pear, orange, lemon ] And use [ ] to access elements too fruit[2] >>> orange Indexing works the same as strings i.e. start with [0] 5/9/17 Matni, CS16, Sp17 3

REVIEW Dictionaries Unordered associative collections Basically lists, but you can access each value by a key instead of an index position Use curly braces, { } to define a dictionary ages = { 'sam':19, 'alice':20 } NOTE THE SYNTAX and the use of the colon key:value KEY VALUE 5/9/17 Matni, CS16, Sp17 4

Measuring Dispersion How much do values vary from the average? Differences from mean: x[i] - mean(x) Includes positive and negative differences So usually square difference: (x[i] - mean(x))**2 Variance: The sum of squared differences (for all i), divided by n 1 Standard deviation = variance sd = n 1 i= 0 ( x[ i] mean( x) ) n 1 2, or square root of 5/9/17 Matni, CS16, Sp17 5

In Python import math def sd(alist): themean = mean(alist) def mean(alist): return(sum(alist)/len(alist)) total = 0 for item in alist: diff = item - themean diffsq = diff ** 2 total = total + diffsq sdev = math.sqrt(total/ (len(alist)-1)) return sdev 5/9/17 Matni, CS16, Sp17 6

Histograms A very popular statistical analysis tool A plot of how often (i.e. frequency) a data point appears in a set 5/9/17 Matni, CS16, Sp17 7

Example Data set is: 1 3 4 1 1 5 9 8 5 6 4 4 5 3 Frequency table: No. Frequency 1 3 3 2 4 3 5 2 6 1 8 1 Histogram Plot: 3 2 1 0 9 1 1 3 4 5 6 8 9 5/9/17 Matni, CS16, Sp17 8

How Can I Do This in Python?! Given: you start with a list of numbers Goal: you have to print out a frequency table Hint: We tackled counting how often numbers occur in lists before! Using dictionaries 5/9/17 Matni, CS16, Sp17 9

Printing a Frequency Table Easiest done with a dictionary (see Listing 4.8): countdict = {} for item in alist: if item in countdict: countdict[item] = countdict[item] + 1 else: countdict[item] = 1 itemlist = list(countdict.keys()) for item in sorted(itemlist): print(item, "\t", countdict[item]) 5/9/17 Matni, CS16, Sp17 10

The One We Came Up With in Class! def freqtable(alist): countd = {} for i in alist: if i not in countd: countd[i] = 1 else: countd[i] = countd[i] + 1 keylist = list(countd.keys()) keylist.sort() print("item", "\t", "Frequency") for x in keylist: print(x, "\t", countd[x]) 5/9/17 Matni, CS16, Sp17 11

How About Plotting That Table? The textbook has an excellent example (listing 4.10). I will leave this as an exercise for you to do in an upcoming lab! 5/9/17 Matni, CS16, Sp17 12

5/9/17 Matni, CS16, Sp17 13

Starting Chapter 5 Files Mostly handle like any sequential data type A sequence of characters if a text file, or a sequence of bytes if a binary file Can you name some file types that are textual? Or binary? 5/9/17 Matni, CS16, Sp17 14

Why Use Files? 4 Good Reasons: Files allow you to store data permanently and conveniently! Data output to a file lasts after the program ends You can usually view them without the need of a Python program An input file can be used over and over No typing of data again and again for testing Files allow you to deal with larger data sets 5/9/17 Matni, CS16, Sp17 15

Files First, open file, and state purpose read or write ThatImportantFile = open('mydata.txt', 'r') MyPrettyOutputs = open('myresults.txt', 'w') Often process text files as a sequence of lines for line in inputfile: # process each line as a string outputfile.write(line[:5] + '\n') Best to close the files when you re done inputfile.close() outputfile.close() DEMO! Let s try it! 5/9/17 Matni, CS16, Sp17 16

More Ways To Read A File Already saw: for line in file to process each line as a separate string (inc. '\n' at ends) To get just a single line (as string): file.readline() Do it again to get the next line, and so on Also can get a list of lines as strings by file.readlines() including '\n' at ends Note readlines vs readline DEMO! Let s try it! 5/9/17 Matni, CS16, Sp17 17

More Ways To Read A File You can also just file.read() to get all of the file s text as a single string Note: use open again if want to go back to the beginning of a file and read from start DEMO! Let s try it! 5/9/17 Matni, CS16, Sp17 18

YOUR TO-DOs q We re just starting Chapter 5, so read ahead 3 THINGS TO FINISH THIS WEEK!!! q Finish Homework5 (due Thursday 5/11) q Finish Lab4 (due Tuesday 5/9) q Keep working on Project1 (due Friday 5/12) q Lab time tomorrow is for working on Project1 q Sing like you mean it 5/9/17 Matni, CS16, Sp17 19

5/9/17 Matni, CS16, Sp17 20