1 of 22

CSE 160 Section 6

Midterm Review!

2 of 22

Logistics

​

Midterm: Friday Oct. 31

​

  • HW3 due October 31
    • Submitting on Gradescope
    • Wait for autograder!
  • Resubmissions due Friday Oct 31 @ 11PM
    • Don’t forget to fill out the resubmission form!
  • Written Check-in 5 due Friday Oct 31
  • Coding Practice 5 due Sunday Nov 2

​

​

​

​

​

3 of 22

Lecture Review: File I/O

4 of 22

File I/O

my_file = open(filepath)

for line_of_text in my_file:

# process line_of_text

my_file.close()

5 of 22

Split

  • Split can be a really helpful function when dealing with strings (which is what you’re processing in file i/o)

Example:

​

O

​

Output:

line_of_text = “you’re going to be amazing”

x = line_of_text.split()

print(x)

​

​

[“you’re”, “going”, “to”, “be”, “amazing”]

6 of 22

Split

  • If you give split an argument, it will split the line of text on that. If you don’t give it anything, it will by default split on spaces.

Example:

​

O

​

Output:

line_of_text = “you’re,going,to,be,amazing”

x = line_of_text.split(“,”)

print(x)

​

​

[“you’re”, “going”, “to”, “be”, “amazing”]

7 of 22

Lecture Review: Dictionary

8 of 22

Dictionary

Collection of key-value pairs

​

  • Key type must be immutable and keys must be unique

​

  • Key-value pair order doesn’t matter (dictionaries are unordered)

​

  • Access value from a key using index syntax (square brackets)

​

  • If you try to access a key not that doesn’t exist in the dictionary, it will throw an error

​

​

​

​

9 of 22

Dictionary

heights = {"Ella": 68,

"Martin": 72,

"Lilly": 49,

"William": 50,

"Simon": 70}

print(heights["Lilly"]) # 49

print(heights["Wen"]) # KeyError

​

heights["Wen"] = 63 # Add a key-value pair

heights["Lilly"] = 50 # Update value

print(heights["Lilly"]) # 50

​

10 of 22

Dictionary

# print out all keys

for key in heights.keys():

print(key)

​

# print out all values

for value in heights.values():

print(value)

​

​

# print out keys and values

for (key, value) in heights.items():

print(key, value)

​

# another method

for key in heights:

value = heights[key]

print(key, value)

​

11 of 22

Lecture Review: Nested Structures

12 of 22

Review of nested structures

  • So far you have seen lists and dictionaries.
  • There are ways to combine them into nested structures in order to represent different types of data.

​

Nested Structure

Example

List of lists

Pixel grids

Dictionaries with lists as values

centroids_dict

List of dictionaries

Excel data with column headers

Dictionary of dictionaries

Excel data with row and column headers

13 of 22

Review of nested structures

  • Why do we care about nested structures?
    • A lot of the data that we work with in real life come in tables, which cannot be represented by a single list or dictionary

​

14 of 22

Nested Lists

  • Useful for representing data in which only the order matters.
  • Can be multidimensional.
  • Used in HW 3 for image data (how would you represent color images?)

​

255

0

255

0

0

255

255

255

255

​

​

​

​

​

​

​

​

​

15 of 22

Midterm Review Questions?

16 of 22

Practice Questions

17 of 22

Problem A

Write a function called report_long_lines that takes a string file_name and an integer max_length as arguments. It should return the number of lines that were longer than the given length.You may assume the file name provided describes a file that exists.

​

Example:

​

numbers.txt:

one

two

three

four

five

​

print(report_long_lines(“numbers.txt”, 3)) Output: 3

​

18 of 22

Solution A

def report_long_lines(file_name, max_length):

num_lines = 0

f = open(file_name, "r")

for line in f:

if len(line) > max_length:

num_lines += 1

close(f)

return num_lines

19 of 22

Problem B

Write code that would loop over this list and create a list of the last names of only the female patients.

​

​

​

​

​

Should return ['Chan', 'Ross']

​

Python Tutor

20 of 22

Solution B

female_patients = []

​

for solo_patient in patients:

if solo_patient[3] == ‘female’:

female_patients.append(solo_patient[1])

​

print(female_patients)

​

21 of 22

Problem C

Now write code to restructure the list so that the inner lists each contain all of the data of one type i.e. first name, last name , etc

​

​

​

​

​

Should return

[ ['Milos', ‘Delia’, ‘Denise’],

[‘Jones’, ’Chan’, ‘Ross’],

[48, 39, 62],

….

[210, 170, 150]]

​

​

​

​

22 of 22

Problem C

Now write code to restructure the list so that the inner lists each contain all of the data of one type i.e. first name, last name , etc

​

new_list = []

For i range(len(patients[0]))

Data_list = []

For patient in patients:

Data_list.append(patient[i])

new_list.append(Data_list)

​

​

​

​