COMP1701-004
fall 2023
lec-18
Oh, Look - December







Let's dive into A4-P2
It's pretty...intimidating.
Advice when faced with big assignments
- Imagine it's complete. What do you SEE when you run it?
- Write a main that fakes it (for now).
- Good place to practice wishful coding.
- Strive to keep things "no typing" as long as possible.
- You want to just run the program to see if it works.
- If you have to type, it's slow and full of friction.
- Hard-code user inputs until the program is done!
- Test as you go as much as possible.
- It's much (much much much) faster in the long run.
You might be thinking,
"I don't know how to work with files yet - how can I even start?!?"
Pffffft.
There are 2 things that you have to do that you already have the skills to do:
- Output results with a given format.
- Calculate a score for a given applicant.
Which one seems easier?
Which one should you start with?
Watch that collaboration, folks.
- Don't "share your screen".
- It'll be very easy to spot collaboration.
Questions?
let's talk about these things today:
⦾ iterating through 2D lists
⦾ a super-important list gotcha


iterating through 2D/nested lists
lists : nested fors
Many problems you deal with in the Land of Software involves data in some sort of tabular format
skinny;10;42;32 lady bug;90;11;15 rickNmorty;48;22;98 D00MZDAY;99;101;12
video game high scores
2 0 2 -5 -3 -2 -5 -5 -5 -2 -11 -4 -8 -4 -6 -14 -6 -8 -12 -4 -3 -3 -13 -15 -6 2 -14 -7 1 -10 0 9 4 -3 5 8 -3 2 0 7
temp ranges over 4 days
First Tube, 8:41 Gotta' Jibboo, 11:54 Foam, 9:09 The Lizards, 10:18 You Enjoy Myself, 20:57
live Phish song durations
G4|--|R2 G3|--|S2 W2|--|W1
dice arrangements
lists : nested fors
Such data can often nicely be represented using lists of lists
skinny;10;42;32 lady bug;90;11;15 rickNmorty;48;22;98 D00MZDAY;99;101;12
video game high scores
2 0 2 -5 -3 -2 -5 -5 -5 -2 -11 -4 -8 -4 -6 -14 -6 -8 -12 -4 -3 -3 -13 -15 -6 2 -14 -7 1 -10 0 9 4 -3 5 8 -3 2 0 7
temp ranges over 4 days
First Tube, 8:41 Gotta' Jibboo, 11:54 Foam, 9:09 The Lizards, 10:18 You Enjoy Myself, 20:57
live Phish song durations
G4|--|R2 G3|--|S2 W2|--|W1
dice arrangements
high_scores = [
["skinny", [10, 42, 32]],
["lady bug", [90, 11, 15]],
["rickNmorty", [48, 22, 98]],
["D00MZDAY", [99, 101, 12]]
]temp_ranges = [
[2, 0, 2, -5, -3, -2, -5, -5, -5, -2],
[-11, -4, -8, -4, -6, -14, -6, -8, -12, -4],
[-3, -3, -13, -15, -6, 2, -14, -7, 1, -10],
[0, 9, 4, -3, 5, 8, -3, 2, 0, 7]
]
song_durations = [
["First Tube", [8, 41]],
["Gotta' Jibboo", [11, 54]],
["Foam", [9, 9]],
["The Lizards", [10, 18]],
["You Enjoy Myself", [20, 57]],
]building = [
["G4", '', "R2"],
["G3", '', "S2"],
["W2", '', "W1"],
]
You don't have to follow the original data format - you can make choices that make your life easier!
QUESTION
How do you suppose you could create these lists if the original data was text?
lists : nested fors
We usually want to walk - or iterate - through all "inner" lists inside the "outer" list and do something
high_scores = [
["skinny", [10, 42, 32]],
["lady bug", [90, 11, 15]],
["rickNmorty", [48, 22, 98]],
["D00MZDAY", [99, 101, 12]]
]temp_ranges = [
[2, 0, 2, -5, -3, -2, -5, -5, -5, -2],
[-11, -4, -8, -4, -6, -14, -6, -8, -12, -4],
[-3, -3, -13, -15, -6, 2, -14, -7, 1, -10],
[0, 9, 4, -3, 5, 8, -3, 2, 0, 7]
]
song_durations = [
["First Tube", [8, 41]],
["Gotta' Jibboo", [11, 54]],
["Foam", [9, 9]],
["The Lizards", [10, 18]],
["You Enjoy Myself", [20, 57]],
]building = [
["G4", '', "R2"],
["G3", '', "S2"],
["W2", '', "W1"],
]
print the number of high scores under 40 for each player
find the average temperature across all days
find the glass score
find the shortest song longer than 10 minutes
lists : nested fors
This iteration is very often done using a nested for loop
high_scores = [
["skinny", [10, 42, 32]],
["lady bug", [90, 11, 15]],
["rickNmorty", [48, 22, 98]],
["D00MZDAY", [99, 101, 12]]
]print the number of high scores under 40 for each player
pseudocode
for every player in the outer list
["skinny", [10, 42, 32]],count the scores < 40 and print
need to loop through these scores and count
need to loop through each player's info
lists : nested fors
high_scores = [
["skinny", [10, 42, 32]],
["lady bug", [90, 11, 15]],
["rickNmorty", [48, 22, 98]],
["D00MZDAY", [99, 101, 12]]
]for curr_player in high_scores:
curr_player_name = curr_player[0]
curr_player_scores = curr_player[1]
under_40_count = 0
for curr_score in curr_player_scores:
if curr_score < 40:
under_40_count += 1
print(curr_player_name, ":", under_40_count)
skinny : 2 lady bug : 2 rickNmorty : 1 D00MZDAY : 1
A Solution
nesting hurts the brain...what could we do to lessen the ouch?
lists : nested fors
high_scores = [
["skinny", [10, 42, 32]],
["lady bug", [90, 11, 15]],
["rickNmorty", [48, 22, 98]],
["D00MZDAY", [99, 101, 12]]
]def num_scores_under_40(scores: list) -> int:
under_40_count = 0
for curr_score in scores:
if curr_score < 40:
under_40_count += 1
return under_40_count
for curr_player in high_scores:
curr_player_name = curr_player[0]
curr_player_scores = curr_player[1]
under_40_count = num_scores_under_40(curr_player_scores)
print(curr_player_name, ":", under_40_count)Functions make things less hurty
It's a bit less painful - and you can test the function separately, which is a plus.
a big list gotcha
a big list gotcha
x = "foo"
y = x
print("1: x is", x)
print("2: y is", y)
x += "bar"
print("3: x is", x)
print("4: y is", y)Predict what is printed
a big list gotcha
x = ["it's", "a", "monorail"]
y = x
print("1: x is", x)
print("2: y is", y)
del x[2]
x.append("trap!")
print("3: x is", x)
print("4: y is", y)Predict what is printed
WHAT DARK WITCHERY IS THIS?!?!
a big list gotcha
x = ["it's", "a", "monorail"]
y = x
print("1: x is", x)
print("2: y is", y)
del x[2]
x.append("trap!")
print("3: x is", x)
print("4: y is", y)Let's use Python Tutor
a big list gotcha
x = ["it's", "a", "monorail"]
loc_of_xs_list = id(x)
y = x
loc_of_ys_list = id(y)
print(f"1: the list x refers to is at {loc_of_xs_list}")
print(f"2: the list y refers to is at {loc_of_ys_list}")
del x[2]
x.append("trap!")
print("3: x is", x)
print("4: y is", y)
id() is a useful function to figure out "who" an object is
lec-18
By Jordan Pratt
lec-18
A4 dive | modifying lists | list return values | a list gotcha
- 175