lec-18
It's pretty...intimidating.
Pffffft.
There are 2 things that you have to do that you already have the skills to do:
Which one seems easier?
Which one should you start with?
⦾ iterating through 2D lists
⦾ a super-important list gotcha
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
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?
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
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
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
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
nesting hurts the brain...what could we do to lessen the ouch?
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)It's a bit less painful - and you can test the function separately, which is a plus.
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)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)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)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)