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

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
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 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
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 2017 Lecture 16: Fencepost Loops and Review
Dr. Kyung Eun Park Summer 2017
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
CSc 110, Autumn 2017 Lecture 23: lists as Parameters
Lecture 16: Lists (cont.) and File Input
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
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
CSC 142 Computer Science II
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
CSc 110, Spring 2017 Lecture 19: more with lists
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
Chapter 8 Arrays Objectives
Chapter 10 Lists.
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
Building Java Programs
Data Structures – 1D Lists
Building Java Programs
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
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
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Lecture 10: Arrays AP Computer Science Principles
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
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
CSc 110, Spring 2018 Lecture 8: input and Parameters
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 8 Arrays Objectives
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
Building Java Programs
Copyright (c) 2017 by Dr. E. Horvath
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Can we solve this problem?
Building Java Programs
Presentation transcript:

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

Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.6 4 days were above average.

Why the problem is hard We need each input value twice: to compute the average (a cumulative sum) to count how many were above average We could read each value into a variable... but we: don't know how many days are needed until the program runs don't know how many variables to declare We need a way to hold a sequence of values (and of course a way to reference them…)

Lists list: a type that holds a sequence of zero or more values. element: One value in a list. index: A 0-based integer used 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] An alternate form when the values are the same: 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

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

Accessing list elements numbers = [0] * 8 numbers[0] = 3 numbers[1] = 99 numbers[2] = 6 x = numbers[0] numbers[x] = 42 numbers[numbers[2]] = 11 # use numbers[2] as index x 3 x index 1 2 3 4 5 6 7 value 99 42 11 numbers

Out-of-bounds Legal indexes to use []: between list's length and the list's length - 1. Note: this is just like strings 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: 3 99 6 42 0 0 11 0 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 len(list) returns 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 value is produced by: len([10,20,30]) len([3,4] * 2)

Lists and for loops You can use the len function to loop through a list counts = [1, 3, 6, 23, 43, 12] for i in range(0, len(counts[]): print(str(counts[i])) + " ", end='') print() # output: 1 3 6 23 43 12 Or, you can also loop directly over lists, just as with strings for number in counts: print(str(number) + " ", end='')

Weather question Use a list to solve the weather problem: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.6 4 days were above average.

Weather answer # Reads temperatures from the user, computes average and # days above average. def main(): days = int(input("How many days' temperatures? ")) temps = [0] * days # list to store days' temperatures sum = 0 for i in range(0, days): # read/store each day's temperature temps[i] = int(input(("Day " + (i + 1) + "'s high temp: "))) sum = sum + temps[i] average = sum / days count = 0 # see if each day is above average for i in range(0, days): if (temps[i] > average): count = count + 1 # report results print("Average temp = " + str(average)) print(str(count) + " days above average")

Weather question 2 Modify the weather program to print the following output: Type in a temperature or "done" to finish Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Day 7's high temp: done Average temp = 44.6 4 days were above average.

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.

Weather 2 answer # Reads temperatures from the user, computes average and # days above average. def main(): print('Type in a temperature or "done" to finish') temps = [] # list to store days' temperatures sum = 0 done = input("Day 1's high temp: ") day = 1 while(done != "done"): # read/store each day's temperature done = int(done) sum = sum + done temps.append(done) done = input(("Day " + str(day + 1) + "'s high temp: ")) day = day + 1 average = sum / day count = 0 # see if each day is above average for i in range(0, day - 1): if (temps[i] > average): count = count + 1 # report results print("Average temp = " + str(average)) print(str(count) + " days above average")

Weather question 3 Modify the weather program to print the following output: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.6 4 days were above average. Temperatures: [45, 44, 39, 48, 37, 46, 53] Two coldest days: 37, 39 Two hottest days: 53, 48

Weather answer 3 # Reads temperatures from the user, computes average and # days above average. def main(): days = int(input("How many days' temperatures? ")) temps = [0] * days # list to store days' temperatures sum = 0 for i in range(0, days): # read/store each day's temperature temps[i] = int(input(("Day " + (i + 1) + "'s high temp: "))) sum = sum + temps[i] average = sum / days count = 0 # see if each day is above average for i in range(0, days): if (temps[i] > average): count += 1 # report results print("Average temp = " + str(average)) print(str(count) + " days above average") print("Temperatures: " + str(temps))) temps.sort() print("Two coldest days: " + str(temps[0]) + ", " + str(temps[1])) print("Two hottest days: " + str(temps[-1]) + ", " + str(temps[-2]))

"list mystery" problem traversal: A sequential processing of the elements of a 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