data = {} for line in open("votes.csv"): state,votes = line.strip().split(",") data[state] = {"votes": int(votes), "pop": 0} for line in open("pop.csv"): state = line.strip().split(",")[4] pop = line.strip().split(",")[5] if state in data: data[state]["pop"] = int(pop) totalpop = 0 totalvotes = 0 for state,stats in data.iteritems(): totalpop += stats['pop'] totalvotes += stats['votes'] num_per = totalpop/float(totalvotes) print "total", totalpop, "votes", totalvotes, num_per closest = 0.0 for state,stats in data.iteritems(): state_per = stats['pop']/float(stats['votes']) worth = num_per/state_per dist = abs(1-worth) print state, worth, dist