# 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.

Note

Discuss topics in this section with classmates.