CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.

Slides:



Advertisements
Similar presentations
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Advertisements

Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Building Java Programs
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Lists Michael Ernst CSE 140 University of Washington.
Lists Ruth Anderson University of Washington CSE 160 Winter
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
Data Structures: Lists
Ruth Anderson University of Washington CSE 160 Spring 2015
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 23: lists as Parameters
Lecture 16: Lists (cont.) and File Input
CSC 108H: Introduction to Computer Programming
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CS 106A, Lecture 19 ArrayLists
while loops; file I/O; introduction to lists
CSc 110, Spring 2017 Lecture 19: more with lists
CSc 110, Spring 2018 Lecture 33: Dictionaries
Ruth Anderson University of Washington CSE 160 Spring 2018
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Ruth Anderson University of Washington CSE 160 Winter 2017
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 23: lists as Parameters
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
Reading and Writing Files
Building Java Programs
CSc 110, Spring 2018 Lecture 10: More Graphics
CSc 110, Autumn 2016 Programming feel like that?
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp
Adapted from slides by Marty Stepp and Stuart Reges
File output; Arrays reading: 6.4 – 6.5, 7.1
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Presentation transcript:

CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges

Assertion example # Assumes y >= 0, and returns x^y def pow(x, y): prod = 1 # Point A while y > 0: # Point B if y % 2 == 0: # Point C x = x * x y = y // 2 # Point D else: # Point E prod = prod * x y -= 1 # Point F # Point G return prod Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. y > 0 y % 2 == 0 Point A SOMETIMES Point B ALWAYS Point C Point D Point E NEVER Point F Point G y > 0 y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G

List functions Function Description append(x) Add an item to the end of the list. Equivalent to a[len(a):] = [x]. extend(L) Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L insert(i, x) Inserts an item at a given position. i is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list. remove(x) Removes the first item from the list whose value is x. Errs if there is no such item. pop(i) Removes the item at the given position in the list, and returns it. a.pop() removes and returns the last item in the list. clear() Remove all items from the list.  index(x) Returns the index in the list of the first item whose value is x. Errs if there is no such item. count(x) Returns the number of times x appears in the list. sort() Sort the items of the list reverse() Reverses the elements of the list copy() Return a copy of the list.

Lists that change size Sometimes we don't know how big we want our list to be when our program starts It can be useful to create an empty list and fill it up. data = [] data.append("hello") data.append("world") print(data) # ['hello', 'world'] How would we insert another word in the middle?

Exercise Write a function called remove_duplicates that takes a sorted list of numbers and removes any duplicates. For example, if it is called on the following list: data = [-2, 1, 1, 3, 3, 3, 4, 5, 6, 78, 78, 79] after the call the list should be data = [-2, 1, 3, 4, 5, 6, 78, 79]

Looping and removing When you loop through a list and remove elements you change the length of the list. This means you need to change your upper bound as you are looping. You must use a while loop when removing items from a list A for i in range loop won't work as it can't adjust when the length of the list changes A for num in data loop won't work as it cannot alter the list.

Solution def remove_duplicates(data): i = 0 while i < len(data) - 1: if data[i] == data[i + 1]: data.pop(i) else: # we don't want to move on i += 1 # to the next element if we # remove as that will me we # will skip the one that # just moved back into the one # we removed's place

Output to files Open a file in write or append mode 'w' - write mode – replaces everything in the file 'a' – append mode – adds to the bottom of the file preserving what is already in it name = open("filename", "w") # write name = open("filename", "a") # append

Output to files name.write(str) - writes the given string to the file name.close() - closes file once writing is done Example: out = open("output.txt", "w") out.write("Hello, world!\n") out.write("How are you?") out.close() text = open("output.txt").read() # Hello, world!\nHow are you? common PrintStream bug: - declaring it in a method that gets called many times. This causes the file to be re-opened and wipes the past contents. So only the last line shows up in the file.