States of Confusion …

USAMap50StatesMapNames

We’re making progress.  You are becoming more comfortable with processing data, storing it in objects, and displaying it in a GUI.  Today we’ll practice those techniques one more time, this time with data about state population.

Head over to HS APPS N: drive and grab the file USA-States-Populations.csv.  I’ve displayed the contents of the file here as well:

Alabama,4785570,4801627,4817528,4833722
Alaska,713868,723375,730307,735132
Arizona,6408790,6468796,6551149,6626624
Arkansas,2922280,2938506,2949828,2959373
California,37333601,37668681,37999878,38332521
Colorado,5048196,5118400,5189458,5268367
Connecticut,3579210,3588948,3591765,3596080
Delaware,899711,907985,917053,925749
District of Columbia,605125,619624,633427,646449
Florida,18846054,19083482,19320749,19552860
Georgia,9713248,9810181,9915646,9992167
Hawaii,1363731,1376897,1390090,1404054
Idaho,1570718,1583930,1595590,1612136
Illinois,12839695,12855970,12868192,12882135
Indiana,6489965,6516336,6537782,6570902
Iowa,3050314,3064102,3075039,3090416
Kansas,2858910,2869548,2885398,2893957
Kentucky,4347698,4366869,4379730,4395295
Louisiana,4545392,4575197,4602134,4625470
Maine,1327366,1327844,1328501,1328302
Maryland,5787193,5840241,5884868,5928814
Massachusetts,6563263,6606285,6645303,6692824
Michigan,9876149,9874589,9882519,9895622
Minnesota,5310337,5347108,5379646,5420380
Mississippi,2970047,2977886,2986450,2991207
Missouri,5996063,6010065,6024522,6044171
Montana,990527,997600,1005494,1015165
Nebraska,1829838,1841749,1855350,1868516
Nevada,2703230,2717951,2754354,2790136
New Hampshire,1316614,1318075,1321617,1323459
New Jersey,8802707,8836639,8867749,8899339
New Mexico,2064982,2077919,2083540,2085287
New York,19398228,19502728,19576125,19651127
North Carolina,9559533,9651377,9748364,9848060
North Dakota,674344,684867,701345,723393
Ohio,11545435,11549772,11553031,11570808
Oklahoma,3759263,3785534,3815780,3850568
Oregon,3837208,3867937,3899801,3930065
Pennsylvania,12710472,12741310,12764475,12773801
Rhode Island,1052669,1050350,1050304,1051511
South Carolina,4636361,4673509,4723417,4774839
South Dakota,816211,823772,834047,844877
Tennessee,6356683,6398361,6454914,6495978
Texas,25245178,25640909,26060796,26448193
Utah,2774424,2814784,2854871,2900872
Vermont,625793,626320,625953,626630
Virginia,8024417,8105850,8186628,8260405
Washington,6742256,6821481,6895318,6971406
West Virginia,1854146,1855184,1856680,1854304
Wisconsin,5689060,5708785,5724554,5742713
Wyoming,564222,567329,576626,582658

The data in this file is organized by state name.  The additional numbers are the populations for 2010, 2011, 2012, and 2013 respectively. You can probably guess your task: To display the data in a GUI table like our AddressBook and ComputerClass data of our recent lessons. As with those assignments, we’ll need two helper classes: State and UnitedStates.

The STATE Class

As you can see from the data file, the State class has two attributes, name and populationlist, which is a Python list that includes four values.  [0] will indicate 2010, [1] will be 2011, [2] will be 2012, and [3] will be 2013.

At this point, we won’t need any methods in this class.

The UNITEDSTATES Class

This class will have two attributes, name (USA),and statelist, which is a Python list of State objects, one for each of the 50 states in the data.

This class will also need three methods, addState(), importFromCSVFile(), and sortStateByPopulation().

importFromCSVFile(filename): This function will import the data from datafile to the class object.  Hint: To create populationlist[]  on the fly, put the four population cells in brackets [cell, cell, cell, cell].

addState(state): This function will append a State object to stateliest[].

sortStatesByPopulation(yearindex): This will sort stateliest[] by the indicated yearindex. We’ll call this method with yearindex = 3 to sort by the most recent population data. Hint: When comparing populations, make sure you float() the values first.

The Output

You goal is to output the states in descending order by 2014 population.  If all goes well, your table should look like this:

statestable

When you program is working, please submit it using this submission link.