Now that we have figured out how to process the data in the file, we can get to work on answering questions about it. One of the interesting questions to explore might be, “Which city has the worst pollution at the PM2.5 level?” The program below figures that out. It uses the maximum value pattern to do this -
max25
will be used to store the highest PM2.5 value we have seen. We also need to remember the name of the city that value was from. We will use
maxCity
to do this and update it every time we find a new
max25
. It isn’t easy to initialize these to match the first record, so we will start
max25
with a tiny value we know will get replaced by the first record.
There is one extra wrinkle we need to worry about. Recall that the data in a text file is stored as ASCII text. So a PM2.5 value in the file would be
"12"
instead of the number
12
. That is a problem because when you compare strings, they are compared alphabetically.
"B"
is greater than
"Apple"
because B comes after A. By the same logic, the string
"2"
is greater than the string
"12"
because 2 is greater than 1. If we convert the strings to numbers, things will work as expected -
2
is smaller than
12
. So we must use
int()
to convert the PM2.5 values to integer numbers.