Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
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.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
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
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
String and Lists Dr. José M. Reyes Álamo.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 15: Strings and Fencepost Loops
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Autumn 2017 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Ruth Anderson University of Washington CSE 160 Spring 2015
Dr. Kyung Eun Park Summer 2017
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
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 110, Spring 2017 Lecture 28: Sets and Dictionaries
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
Building Java Programs
Building Java Programs
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
Building Java Programs Chapter 7
CSc 110, Spring 2017 Lecture 19: more with lists
CSc 110, Spring 2018 Lecture 33: Dictionaries
Bryan Burlingame Halloween 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
String and Lists Dr. José M. Reyes Álamo.
Can we solve this problem?
python.reset() Also moving to a more reasonable room (CSE 403)
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
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Lecture 10: Arrays AP Computer Science Principles
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Bryan Burlingame Halloween 2018
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Can we solve this problem?
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Python List.
Presentation transcript:

Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Autumn 2017 Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges

Least favorite Favorite Handwritten exams Quizzes Time of deadlines Daily problems No collaboration Problem solving Joy of getting a problem right Programming Resources Projects Cartoons "The projects are probably one of the most satisfying pieces of schoolwork I've ever done. I just feel a real sense of accomplishment when I complete one."

Lists list: object that stores many values. element: One value in a list. index: A 0-based integer to access an element from an list. index -10 1 -9 2 -8 3 -7 4 -6 5 -5 6 -4 7 -3 8 -2 9 -1 value 12 49 26 17 84 72 element 0 element 4 element 9

List initialization name = [value, value, … value] Example: numbers = [12, 49, -2, 26, 5, 17, -6] Useful when you know what the list's elements will be name = [value] * count numbers = [0] * 4 index 1 2 3 4 5 6 value 12 49 -2 26 17 -6 index 1 2 3 value

List initialization name = [value]* size 2 4 26 Example: numbers = [0]*3 Creates the following list index value 2 4 26

Accessing elements name[index] # access name[index] = value # modify Example: numbers = [0] * 2 numbers[0] = 27 numbers[1] = -6 print(numbers[0]) if (numbers[1] < 0): print("Element 1 is negative.") index 1 value 27 -6

Out-of-bounds Legal indexes to use []: between – list's length and the list's length - 1. Reading or writing any index outside this range with [] will cause an IndexError: list assignment index out of range Example: data = [0] * 10 print(data[0]) # okay print(data[9]) # okay print(data[-20]) # error print(data[10]) # error index 1 2 3 4 5 6 7 8 9 value

Lists and for loops It is common to use for loops to access list elements. for i in range(0, 8): print(str(numbers[i]) + " ", end='') print() # output: 0 4 11 0 44 0 0 2 Sometimes we assign each element a value in a loop. numbers[i] = 2 * i index 1 2 3 4 5 6 7 value 8 10 12 14

len() Use len() to find the number of elements in a list. for i in range(0, len(numbers)): print(numbers[i] + " ", end='') # output: 0 2 4 6 8 10 12 14 What expressions refer to: The last element of any list? The middle element?

Lists and for loops You can also loop directly over lists, just as with strings list = [1, 3, 6, 23, 43, 12] for number in list: print(str(number + " ", end='') print() # output: 1 3 6 23 43 12

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.

"list mystery" problem traversal: An examination of each element of an list. What element values are stored in the following list? a = [1, 7, 5, 6, 4, 14, 11] for i in range(0, len(a) – 1): if (a[i] > a[i + 1]): a[i + 1] = a[i + 1] * 2 index 1 2 3 4 5 6 value 7 10 12 8 14 22 index 1 2 3 4 5 6 value