# Another version of the state population dictionary.
# Identical to the program from Wds, until the last loop.
# This time we write an output file containing the states and the
# number of electoral college votes per million people.
# After that, we put the data into a list of lists, so that we
# can sort it.
# Finally we print out the sorted list.
# So this program is built with 5 loops!
# Two reading, one writing, one dumping the dictionary of lists
# to a list of lists, and finally one printing out the list of lists. 

# Open the input data file for reading
stateFile = open("StatePopulations.txt","r")

# Begin with the empty dictionary
popDict = {}
for line in stateFile:
    # split on whitespace; wordList is now a list of words
    wordList = line.split()
    # Ignore empty lines
    if wordList == []:
        continue
    # Ignore territories
    if wordList[0] != "n/a":
        if len(wordList) == 11:
            state = wordList[2]
            population = wordList[3]
        else:
            state = wordList[2]+" "+wordList[3]
            population = wordList[4]
	# Store the population as the value, with stateName as the key
        population = population.replace(",","")
        pop = float(population)
        popDict[state] = [pop]


stateFile.close()
stateFile = open("StatePopulations.txt","r")

for line in stateFile:
    # split on whitespace; wordList is now a list of words
    wordList = line.split()
    # Ignore empty lines
    if wordList == []:
        continue
    # Ignore territories
    if wordList[0] != "n/a":
        if len(wordList) == 11:
            state = wordList[2]
            voteStr = wordList[5]
        else:
            state = wordList[2]+" "+wordList[3]
            voteStr = wordList[6]
	# Store the population as the value, with stateName as the key
        votes = float(voteStr)
        popDict[state] = popDict[state]+[votes]

stateFile.close()

# The new stuff starts here.
# We first open the output file, and as the first line of the
# output file we write headings for the two columns. 
outFile = open("ratioData.txt","w")
outFile.write("State \t Votes per Million \n")
# A for loop on the dictionary. The variable state will contain each
# of the keys in the dictionary in turn.
for state in popDict:
    # use the key to look up the data value (list of numbers)
    dataList = popDict[state]
    pop = dataList[0]
    votes = dataList[1]
    ratio = (votes/pop)*1000000
    # to write to the output file, use the write method.
    # I writes a single string.
    outFile.write(state+"\t"+str(ratio)+"\n")

# close the file; you won't see the data in the file until it is closed. 
outFile.close()

# A second loop that dumps data out of the dictionary. 
LoL = [] # LoL stands for List of Lists. 
for state in popDict:
    dataList = popDict[state]
    pop = dataList[0]
    votes = dataList[1]
    ratio = (votes/pop)*1000000
    # Put everything into the data list - state and ratio as well as
    # population and number of votes. 
    dataList = [ratio] + [state] + dataList
    # Then put the datalist for this state into the list of lists
    LoL = LoL + [dataList]

# Sort the list of lists by ratio.
LoL.sort()

# Tiny for loop, to print out the list of lists, one list per line.
for data in LoL:
    print data







    
