18.8. Finding the Pollution for a State

We can use split to chop up our data by fields, separated by “:”. But we can also use it within field, to separate by space. If we split the city/state by “space”, we’d get city and state as two different elements of the list.

This section uses the same data file that we have been using, but if you want to see all of the data click on the Show button below. Once it appears, you can hide it again by clicking on the Hide button.

But, what if the city name has a space in it? Then we won’t get the state in the second slot. It is safer to split on ,.

So, to check for particular state, we want to know if the current state name matches the one we’re looking for. We can use the strName.find(test) function to see if the current state is the one we are looking for. The find function returns the index of the test in strName or -1 if it isn’t found.

Now, let’s reuse the average code and look for a particular state.

Use the below active code area to figure out the PM 10 code. Replace the lines that start with #.

What states have the highest average pollution values? What do you think is most related to pollution: Population, area, or wealth? Does it differ by PM 2.5 and PM 10? For a list of the state two-letter abbreviations see http://www.50states.com/abbreviations.htm#.VJCB9r5NsXc.

The following program prints the average PM 2.5 pollution for a state, but the code is mixed up. Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on <i>Check Me</i> to see if you are right. You will be told if any of the lines are in the wrong order or have the wrong indention.

        # read all the lines
infile = open("uspoll.txt","r")
lines = infile.readlines()
infile.close()

# initialize the variables
state = "CA"
total25 = 0
count = 1.0
---
# loop through the lines
for line in lines:
---
    # split at :
    values = line.split(":")
---
    # split at ,
    cityState = values[0].split(",")
---
    # if found state
    if cityState[1].find(state) >= 0:
---
        # add the current to the sum
        new25 = float(values[2])
        total25 = total25 + new25

        # increment the count
        count = count + 1
---
# print the average
avg = total25/count
print("Avg for " , state, " is ", avg)
        

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted of activities on this page