Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Similar documents
Module 06. Topics: Iterative structure in Python Readings: ThinkP 7. CS116 Spring : Iteration

MEIN 50010: Python Data Structures

Module 8: Local and functional abstraction

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Module 3: Strings and Input/Output

CS116 - Module 5 - Accumulative Recursion

Lists How lists are like strings

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Lessons on Python Functions

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Module 05: Types of recursion

Module 08: Searching and Sorting Algorithms

Module 10: Imperative Programming, Modularization, and The Future

Module 07: Efficiency

University of Texas at Arlington, TX, USA

Introduction to Problem Solving and Programming in Python.

The Practice of Computing Using PYTHON

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Module 01: Introduction to Programming in Python

Sequences and iteration in Python

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Using Scala in CS241

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

cs1114 REVIEW of details test closed laptop period

python 01 September 16, 2016

OOP and Scripting in Python Advanced Features

Ch.2: Loops and lists

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

MUTABLE LISTS AND DICTIONARIES 4

CS115 - Module 9 - filter, map, and friends

MITOCW watch?v=rvrkt-jxvko

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Lists and Loops. browse Python docs and interactive help

Introduction to Python! Lecture 2

CIS192 Python Programming

Python Intro GIS Week 1. Jake K. Carr

An introduction introduction to functional functional programming programming using usin Haskell

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Python Review IPRE

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Scheme as implemented by Racket

Variable and Data Type I

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Patterns The Essence of Functional Programming

Python: Short Overview and Recap

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

Lists, loops and decisions

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community

Python Programming: Lecture 2 Data Types

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

List Processing Patterns and List Comprehension

CS1 Lecture 12 Feb. 11, 2019

Lecture 7: Python s Built-in. in Types and Basic Statements

DM502 Programming A. Peter Schneider-Kamp.

Shell CSCE 314 TAMU. Functions continued

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

CS 116 Fall 2016 Review Notes Created By: Rina Dong

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Advanced Python. Executive Summary, Session 1

Programming with Python

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

Module 09: Additional Options for Organizing Data

Intro. Scheme Basics. scm> 5 5. scm>

Python Lists. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 5, 2011

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Introduction to programming using Python

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Introduction to Python, Cplex and Gurobi

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Working with recursion

Variable and Data Type I

List Functions, and Higher-Order Functions

CPSC 217 Midterm (Python 3 version)

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CS-141 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community

Here n is a variable name. The value of that variable is 176.

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Python Programming Exercises 3

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Topic 7: Lists, Dictionaries and Strings

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Beyond Blocks: Python Session #1

At full speed with Python

Transcription:

Module 04: Lists Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10 1

Consider the string method split >>> name = "Harry James Potter" >>> name.split() ['Harry', 'James', 'Potter'] >>> name.split('e') ['Harry Jam', 's Pott', 'r'] split returns a list of strings. 2

Lists in Python Like Racket lists, Python lists can store any number of values any types of values (even in one list) Creating lists: Use square brackets to begin and end list, and separate elements with a comma Concatenate (using +) existing lists to create a new list Examples: num_list = [4, 5, 0] str_list = ['a', 'b'] empty_list = [] mixed_list = ['abc', 12, True, '',-12.4] 3

Useful Information about Python Lists len(l)=> number of items in the list L L[i]=> item at position i Called indexing the list Causes an error if i is out of range Positions: 0 <= i < len(l) Actual valid range: -len(l) <= i < len(l) "Slicing" a list L[i:j]=>[L[i],L[i+1],,L[j-1]] L[i:j:k]=>[L[i],L[i+k],,L[i+m*k]], includes all the positions up to (but not including) j 4

Basic Template for Recursion def list_template(l): if L == []: # base case action else: # L[0] (first) # list_template (L[1:]) # (recurse on rest) 5

Example: Write a recursive Python function build_str that consumes a list of strings (los), and creates and returns a new string by concatenating together all the strings in los. Aside: The following operation also solves this problem: "".join(los) 6

Other list operations in x in L => True if x is in L, False otherwise 5 in [10,2,4,5] => True "a" in ["hello", "there", "anyone"] => False sum Returns the sum of all values in a list of numbers sum([]) => 0 sum([1,2.25,0,-1]) => 2.25 sum([0,1,2,'3']) => Error 7

Other list operations >>> dir(list) [, 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Most of these methods change the contents of list. None of these methods return a new list. 8

Using list methods What does this do? L = [1,2,3] v = L.pop(0) L.append(v) To fully investigate how we can change the contents of a list using the list methods or other techniques, we first need to learn about list mutation. 9

Mutation and Lists Once a list is created We can change individual values in the list We can add values to the list We can remove values from the list Lists are mutable, unlike the other values we have studied so far. To fully understand how lists can be mutated, we need to learn the basics about how memory is managed in Python. We will use a simplified model. 10

Python Memory Model: Initialization using immutable values Consider: var = expr where expr simplifies to Int, Float, Str, or Bool A box is created and labelled var expr is simplified to a value, and put inside the box For example: x = 2 y = 2-5 z = "a" + "b" x y 2-3 z "ab" CS116 Fall 2018 04: Lists 11

Python Memory Model: Changing an existing variable Consider: var = new_expr where var already has a value, and new_expr simplifies to an immutable value new_expr is simplified to a value, and put into the box, overwriting previous contents For example: x = 2 x = x+1 x 2 3 32 CS116 Fall 2018 04: Lists 12

Python Memory Model: More Basics Suppose the value of one variable is used to initialize another variable: x = 5 y = x The value in x's box is copied to y's box x y 5 5 CS116 Fall 2018 04: Lists 13

Python Memory Model: more If a new value is assigned to one of the variables, the variables no longer share a value x = 5 y = x x = 'a' Only one variable's value is changed. The other is unchanged. x 5 "a" y 5 CS116 Fall 2018 04: Lists 14

Representing lists in memory L = list_expr Lists are comprised of multiple values, and list components can be changed, so the representation is more complicated The simplified value of list_expr is not put in L's box: it gets its own space in memory which L will reference 15

Representing lists in memory p = [] m = [1,0,3] p m 1 0 3 CS116 Fall 2018 04: Lists 16

L = [1,2,'abc'] L[1] = 3 L[0] = L[0]-L[1] L[2] = True Mutation and Lists L 1-2 2 3 'abc' True CS116 Fall 2018 04: Lists 17

Other ways to mutate a list L = [3,0] L 3 0 L.append(-100) WARNING: Do NOT write L = L.append(-100) L 3 0-100 CS116 Fall 2018 04: Lists 18

More ways to mutate a list L.extend(['a','b',0]) L 3 0-100 'a' 'b' 0 L.insert(3, True) L 3 0-100 True 'a' 'b' 0 CS116 Fall 2018 04: Lists 19

Even more ways to mutate a list L.remove(0) L 3-100 True 'a' 'b' 0 L.pop(4) 'b' L 3-100 True 'a' 0 CS116 Fall 2018 04: Lists 20

m = [1,2] p = m Sharing list values m 1 2 P When two variables point to a common value in memory, they are called aliases. CS116 Fall 2018 04: Lists 21

Consequences of aliasing If two variables are aliases of a common list, you can mutate the existing list using either variable name m = [1,2] p = m m 1 5 2 27 m[0] = 5 p p[1] = 27 CS116 Fall 2018 04: Lists 22

m = [1,2] p = m m = ['x'] m p CS116 Fall 2018 Can we break aliases? 'x' 1 2 04: Lists Yes! Just assign a new value to one of the variables. Only that variable is changed. 23

Recall: Functions and Immutable Parameters def change_to_1(n): n = 1 grade = 89 change_to_1(grade) n grade 89 1 89 CS116 Fall 2018 04: Lists 24

Functions and List Parameters def change_first_to_1(l): L[0] = 1 L my_list = ['a', 2, 'c'] change_first_to_1(my_list) my_list 'a' 1 2 'c' CS116 Fall 2018 04: Lists 25

What is different here? def change_second_to_1(l): L = [L[0],1] + L[2:] return L my_list = [100,True,0] my_list 100 True 0 CS116 Fall 2018 04: Lists 26

What is different here? def change_second_to_1(l): L = [L[0],1] + L[2:] return L my_list = [100,True,0] my_list 100 True 0 M = change_second_to_1(my_list) L 100 1 0 M CS116 Fall 2018 04: Lists 27

Exercise: Memory management def dec(t): t = t - 1 return t n = 1 m = n n = 2*m L = [3,6,9] Q = L p = L[2] Q[0] = dec(m) L[1] = dec(q[2]) L[2] = [True, False] m = L.append(n) L = "x" 28

n m L Q 2 None "x" Completed exercise True False 0 8 2 p 9 CS116 Fall 2018 04: Lists 29

When writing a function with lists Important to determine if a function is supposed to Use the values in an existing list, Mutate an existing list, or Create and return a new list Review ThinkP 10.12 30

Testing Mutation For each test: 1. Set values of variables for testing 2. Call the appropriate check function to compare actual returned value to expected returned value (which might be None) 3. Call the appropriate check function on each testing variable that has been mutated, comparing the actual value to the expected value after mutation. 31

Design recipe additions If a function mutates the contents of a parameter, this must be included in the Purpose and Effects statements. Possible effects of a function: Printing to screen Reading from keyboard Mutation of parameter 32

Example: Mutation import check import math def multiply_first(l, factor): L[0] = L[0] * factor ## Test 1: factor = 0 L = [10,-2,3] check.expect("t1", multiply_first(l,0), None) check.expect("t1{l}", L, [0,-2,3]) ## Test 2: factor not an integer (pi) L = [10,0,-3.25] check.expect("t2", multiply_first(l,math.pi), None) check.within("t2(l)", L, [31.415926,0,-3.25], 0.00001) 33

Example: multiply_by Use recursion to complete the Python function multiply_by that consumes a list of integers (vals) and another integer (multiplier) and mutates vals by multiplying each value in it by multiplier. The function returns None. 34

Example: multiply_by def multiply_by(vals, multiplier): '''multiplies each value in vals by multiplier Effects: mutates vals multiply_by:(listof Int) Int-> None Example: for L = [1,2,3], multiply_by(l, 10) => None, and changes contents of L to [10,20,30]''' 35

Lists can be nested Consider the list L = [[1,2], [], [7,8,9,10]] What are the values of: len(l) L[0] len(l[1]) L[2][3] How do we negate the entry 10? 36

Working with nested lists def sum_firsts(lol): '''returns the sum of all the first values in the nonempty lists in lol sum_firsts: (listof (listof Int)) => Int Example: sum_firsts([[1,2],[],[7,8,9]]) => 8 ''' if lol == []: return 0 elif lol[0] == []: return sum_firsts(lol[1:]) else: return lol[0][0] + sum_firsts(lol[1:]) 37

Other Relevant List Information Retrieving the first element in a list is as fast as retrieving the last element In many other languages: Lists are of a fixed size once created Lists can only contain one type of value Processing these lists (often called arrays) tends to be faster than processing Python lists Python has an array module (not used in CS116) 38

Functional Abstraction in Python Abstract list functions in Python 3 return iterators Use the functions map and filter as you did in Racket, but cast the value returned to a list. list(map(fn, lst)) list(filter(fn, lst)) 39

Functional Abstraction in Python: map map(function, lst) returns a new iterator, applying function to each element in list Requires: type consumed by function must match type in lst ------------------------------------------------------------------------------- def pull_to_passing(mark): if mark < 50 and mark > 46: return 50 else: return mark list(map(pull_to_passing, [34, 89, 46, 49, 52])) => [34, 89, 46, 50, 52] 40

Functional Abstraction in Python: filter filter(function, lst) returns an iterator matching the elements in list for which function returns True Requires: type consumed by function must match type in lst ------------------------------------------------------------------------ def big_enough(mark): return mark>50 list(filter(big_enough, [34, 89, 46, 49, 52])) => [89, 52] 41

Using map and filter Both consume a function and a list The type consumed by the parameter function must match the type of values in the parameter list Sometimes, we define a function and it is only used in a call to map or filter We can avoid this by using a lambda expression 42

lambda Like Racket, Python allows for anonymous functions using lambda Syntax: lambda x: body lambda x,y: body Note that body should be an expression, not a statement 43

Examples def starters(words, start): return list(filter(lambda s: s[:1]==start, words)) def expts_of(base, exponents): return list(map(lambda k: base**k, exponents)) def cap_a(words): return list(map(lambda s: 'A'+s[1:], list(filter(lambda s: s[:1]=='a', words)))) 44

Using range with map and filter To create a list we may use range: list(range(6)) => [0,1,2,3,4,5] list(range(3,7)) => [3,4,5,6] list(range(4,15,3)) => [4,7,10,13] This is extra powerful with map and filter: def sqr(x): return x*x list(map(sqr, range(6))) => [0, 1, 4, 9, 16, 25] 45

map and filter can operate on strings as well def just_digits(word): return list(filter( lambda c:c.isdigit(), word)) Note: Even when processing strings, map and filter return an Iterator, which should be converted to a list. 46

Review: when functions consume lists as parameters Function definition: def f(l): Function call: f(a) If f makes an assignment directly to L, then A is not changed. (L = ) If f makes an assignment to an element of L, or mutates L using list methods, then A is changed as well. (L[0] =, L.extend( )) 47

def fn_two(l,m,x): '''fn_two:(listof Y)(listof Z) X-> None''' x = 10 L = "Howdy" M[0] = 'abc' M.append(x) # Call the function A = [] B = [1,2,3] z = 42.42 fn_two(a,b,z) print(a, B, z) CS116 Fall 2018 04: Lists 48

Goals of Module 04 We should now be able to write any of our Racket programs in Python, using Lists and their methods Lists used to implement structures Mutation of lists Functional abstraction and lambda 49