COMP1701-004
fall 2023
lec-20
Just a Wee Bit Left





SPoTs
A4 Questions?
let's talk about these things today:
⦾ reading from text files (cont'd)
⦾ object references
⦾ slicing
⦾ range
reading from text files
method 2
use a for loop to walk through each line in a file
cont'd from last week
- Process all the records and calculate some value as you go.
- Turn all/some of the records into a list that you can use for other purposes.
- Process individual records, immediately doing something with each result.
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
...The file we were using was called incidents.csv and had records that looked like this.
incidents.csvlocation info
description
start date/time
city quadrant
longitude
latitude
fields
We had done this task already:
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 does- Process all the records and calculate some value as you go.
- Turn all/some of the records into a list that you can use for other purposes.
- Process individual records, immediately doing something with each result.
TASK
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
We'll start off the same way, but this time, we want to gather all the desired records into a list and return it.
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
- Process all the records and calculate some value as you go.
- Turn all/some of the records into a list that you can use for other purposes.
- Process individual records, immediately doing something with each result.
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.
- Process all the records and calculate some value as you go.
- Turn all/some of the records into a list that you can use for other purposes.
- Process individual records, immediately doing something with each result.
I'm fine with either one - you won't lose marks if you choose 2!
🙋🏻♂️❓🙋🏻♀️Why might 3 be "better" in A4?


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
Not very surprising, right?
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"]
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)
id() is a useful function to figure out "who" an object is
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)From last year's final exam!
- What is the value of numbers after line 16 runs?
- What is the value of match_indexes after line 16 runs?
- There is something a bit odd about the function’s behavior in light of its name. In one sentence, explain clearly.

slicing
🙋🏻♂️❓🙋🏻♀️
We've encountered 2 data types representing "sequences of things" in the course. What are they?
Sometimes, a problem you're solving needs only part of a sequence.
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 |
|---|
5
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?How can we slice with a list?
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?
A useful characteristic of slice
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
range()
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([]))Let's just play with it to see what it does
lec-20
By Jordan Pratt
lec-20
reading from files cont'd | references | slicing | range
- 200