#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/11/2022 DS 2000 Lecture 10 - lists of lists Logistics: - HW 4 is due on *tonight* at 9pm -> no late submissions will be accepted - Quiz 5 is available on Gradescope (due Friday @ 9:50am) - Homework 5 is available on the course website (due *next* Friday 10/21 @ 9pm) - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate (please do one of these!) 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) Warm-up 1 - functions & scope 1 --- What is the value of x at the marked point in the following code? """ # def my_subtract(x, y): # # x is set to 5, lives in my_subtract # # y is set to 3, lives in my_subtract # # ans = 5 - 3 # # ans = 2 # ans = x - y # # HERE # print("in subtract", x) # 5 # # return 2 # return ans # def main(): # a = 3 # create a variable a that lives in main() # b = 5 # create a variable b that lives in main() # # x = my_subtract(5, 3) # # x = 2 # x = my_subtract(b, a) # print("in main", x) # 2 # main() # variables are only accessible inside the functions # that they were declared in """ A. 3 B. 5 <----- C. -2 D. 2 E. 0 Warm-up 2 - functions & scope 2 --- What is the value of x at the marked point in the following code? """ # def my_subtract(x, y): # # x in my_subtract 3 # # y in my_subtract 5 # # x = 3 - 5 # x = x - y # # return -2 # return x # def main(): # x = 3 # y = 5 # # a = my_subtract(3, 5) # # a = -2 # a = my_subtract(x, y) # # HERE # # x = .... # print("in main", x) # main() """ A. 3 <---- B. 5 C. -2 D. 2 E. 0 """ """ functions: syntax reminders --- (Deitel & Deitel 4.2) def function_name(parameters, separated, by, commas): # code return value # to call the function answer = function_name(values, matching, in, number) """ """ A function to get part of a list --- Write a function, sublist, that takes two integer parameters a start index (inclusive) and an end index (exclusive) and one list parameter that copies the sublist from the given list and returns it as a new list. Use a while or for loop. Example function calls smaller = sublist(0, 2, [5, 7, 3, 4, -3]) # [5, 7] smaller = sublist(1, 3, [5, 7, 3, 4, -3]) # [7, 3] smaller = sublist(3, 4, [5, 7, 3, 4, -3]) # [4] """ def sublist(start, end, ls): new_ls = [] # range(start, end) for i in range(start, end): # print(i) new_ls.append(ls[i]) return new_ls print("Sublist function") smaller = sublist(0, 2, [5, 7, 3, 4, -3]) print(smaller) smaller = sublist(1, 3, [5, 7, 3, 4, -3]) print(smaller) smaller = sublist(3, 4, [5, 7, 3, 4, -3]) print(smaller) print() """ Slicing (lists and strings) --- Return the sublist as a new list to you sublist = ls[start (inclusive): end (exclusive)] """ print("List slicing") nums = [5, 7, 3, 4, -3] print(nums[0:2]) print(nums[1:3]) print(nums[3:4]) print(nums[:3]) # start at 0, go to index 3 (exc) print(nums[3:]) # start at 3, go to end print(nums[:]) # copies the list print() """ Lists of lists --- A list of lists is a "table" of data: neighborhood,latitude,longitude "Charlestown",42.3787,-71.0593 "Allston / Brighton",42.3596,-71.1466 "Back Bay",42.3594,-71.0587 "South Boston",42.3337,-71.0466 """ print("List of list examples") # syntax examples data = [["Charlestown",42.3787,-71.0593], ["Allston / Brighton",42.3596,-71.1466], ["Back Bay"], ["South Boston",42.3337,-71.0466]] # get the first row in the data print(data[0]) # get the third row in the data print(data[2]) # get the number of rows in the data print(len(data)) # get the number of columns in the data print("cols in row 0:", len(data[0])) print("cols in row 2:", len(data[2])) data[0].append("EXTRA FUN") print(data) print("cols in row 0:", len(data[0])) # to get a column value out # data[row index][col index] print(data[0][0]) print(data[2][0]) print() """ Verify all rows same length --- Write a function, verify_tabular, that returns True if all rows in a list of lists have the same length and False otherwise. """ def verify_tabular(ls_of_ls): # special python value that represents nothing target_length = None for row in ls_of_ls: # will be True on the first row if target_length is None: target_length = len(row) elif len(row) != target_length: return False return True print("verify tabular: ") print(verify_tabular(data)) # False print(verify_tabular([[1, 2], [3, 4], [5, 6]])) # True print(verify_tabular([[1], [3]])) # True print(verify_tabular([])) # True print() """ Lists of lists and nested loops (example 1) --- Write code that sums all numbers in a 2-dimensional list of lists of numbers. Do this without using the python sum() function. """ nums = [[1, 2], [3, 4], [5, 6]] total = 0 # outer loop does rows for row in nums: # inner loop does columns within the row for col_value in row: print(col_value) total += col_value print("total", total) print() """ Lists of lists and nested loops (example 2) --- Write code that capitalizes all words in a 2-dimensional list of lists of strings. """ words = [["cat", "dog"],["turtle"]] # ls[index] = value # words[row index][col index] = value for row_index in range(len(words)): for col_index in range(len(words[row_index])): words[row_index][col_index] = words[row_index][col_index].upper() print(words) print() """ Example program - updating earnings.py --- Update earnings.py so that we only read the data in once, into a list of lists, then extract the target column from the list of lists rather than re-opening the underlying file each time. Add a search functionality to search for titles that contain some substring. (The data comes from here, originally: https://data.boston.gov/dataset/employee-earnings-report) """ """ Next time: - data: where does it come from? - more on scope - lists + mutation """