lec-20
⦾ reading from text files (cont'd)
⦾ object references
⦾ slicing
⦾ range
We began talking about 3 scenarios that are common when iterating through a file that contains multiple records:
Westbound 16 Avenue at Deerfoot Trail NE ,Stalled vehicle. Partially blocking the right lane,6/21/2022 7:31,NE,-114.0266867,51.06748513
11 Avenue and 4 Street SW ,Traffic incident. Blocking multiple lanes,6/21/2022 4:02,SW,-114.0714806,51.04262449
68 Street and Memorial Drive E ,Traffic incident.,6/20/2022 23:53,NE,-113.9355533,51.05247351
...incidents.csvlocation info
description
start date/time
city quadrant
longitude
latitude
fields
TASK
Find the number of traffic incidents in Calgary in 2023 that involved a cyclist.
..and (you should) have completed this:
file_reader = open("incidents.csv", "r")
num_cyclist_incidents = 0
for line in file_reader:
# 1) see if line involves a cyclist and...
# 2) ...bump up our variable if it doesTASK
Create a list of all the incidents in the file that took place in the SW. Assume we're only interested in the latitude and longitude of those incidents, so our list should only have those things.
Let's move on to the second scenario
file_reader = open("incidents.csv", "r")
result = ???
for line in file_reader:
# 1) if the record is of interest...
# 2) ...put the lat/lon in the result
# close the reader before returning!
return resultPitter patter.
🙋🏻♂️❓🙋🏻♀️What's this pattern called?
imagine this is the body of a function
TASK
Write a summary report - summary.txt - of all incident descriptions that took place in December.
And now, the final scenario
🙋🏻♂️❓🙋🏻♀️
While we could build a list of the desired information and then write that list...do we need to?
file_reader = open("incidents.csv", "r")
summary_writer = ???
for line in file_reader:
# 1) if the record is of interest...
# 2) ...write what's needed to file
# close BOTH file minions!Both scenario 2 and 3 are reasonable things to do on A4, BTW.
I'm fine with either one - you won't lose marks if you choose 2!
🙋🏻♂️❓🙋🏻♀️Why might 3 be "better" in A4?
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)Not very surprising, right?
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"]
id_of_xs_list = id(x)
y = x
id_of_ys_list = id(y)
print(f"1: the list x refers to is {id_of_xs_list}")
print(f"2: the list y refers to is {id_of_ys_list}")
del x[2]
x.append("trap!")
print("3: x is", x)
print("4: y is", y)
def find_all_in_list(
a_list: list, matching_indexes:
list, item: int
) -> None:
found = False
index = 0
while not found and index < len(a_list):
if a_list[index] == item:
found = True
matching_indexes.append(index)
else:
index += 1
numbers = [1, 2, 1, 3]
match_indexes = []
find_all_in_list(numbers, match_indexes, 1)🙋🏻♂️❓🙋🏻♀️
We've encountered 2 data types representing "sequences of things" in the course. What are they?
example
You have a string with a full name...but only want the first name.
example
You have a sorted list of race results...but only want the top 3.
full_name = "Hange Zoë"
# How do we get just "Hange"?race_results = [
["1A-412", "Renzer", "1:21:35"],
["7X-197", "Arcel", "1:21:39"],
["5F-883", "Bast", "1:22:01"],
# many more entries
]
# How do we get just the first 3?full_name = "Hange Zoë"
# How do we get just "Hange"?| H | a | n | g | e | Z | o | ë |
|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|
We want everything from index 0 to index 4 - the index just before the space
full_name = "Hange Zoë"
first_name = full_name[0:5]slice operator
🙋🏻♂️❓🙋🏻♀️ Figuring out the 0 is easy enough. But we've got a problem. What is it?
Recall that a string is an indexed sequence of characters.
We can find the index of something with the (well-named!) index method.
full_name = "Hange Zoë"
index_of_space = full_name.index(' ')
first_name = full_name[0:index_of_space]| H | a | n | g | e | Z | o | ë |
|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|
full_name = "Hange Zoë"race_results = [
["1A-412", "Renzer", "1:21:35"],
["7X-197", "Arcel", "1:21:39"],
["5F-883", "Bast", "1:22:01"],
# many more entries
]
# How do we get just the first 3?Same deal!
race_results = [
["1A-412", "Renzer", "1:21:35"],
["7X-197", "Arcel", "1:21:39"],
["5F-883", "Bast", "1:22:01"],
# many more entries
]
top_3 = race_results[?:?]🙋🏻♂️❓🙋🏻♀️ What slice values will we use if we want the first 3 entries?
The difference between the right and left indices in the slice operator equals the number of things you get back from the slice!
first_name = full_name[0:5]
top_3 = race_results[0:3]
full_name[2:4] # number of characters?
race_results[1:6] # number of results?5 - 0 = 5
3 - 0 = 3
A function I introduce with some reluctance.
for i in range(5):
print(i)
print()
for i in range(3,7):
print(i)
print()
for i in range(7,3,-1):
print(i)
print()
print(type(range(4)))
print(type([]))