#!/usr/bin/python # -*- coding: utf-8 -*- import copy # Import the election results and group them by riding import csv ridings = dict() parties = dict() total_votes = 0 reader = csv.reader(open('EventResults.csv', 'rb'), delimiter='\t') for row in reader: if row[1] not in ridings: ridings[row[1]] = {} if row[3] == "validated": parties[row[8]] = 0 ridings[row[1]][row[5]] = row total_votes += int(row[10]) # Store the winner of each riding and transfer all other ballots to the respective parties seats = dict() for riding, riding_results in ridings.iteritems(): ordered = sorted(riding_results.items(), key=lambda x: -int(x[1][10])) seats[riding] = (ordered[0][0], ordered[0][1][8], int(ordered[0][1][10])) for k, v in ordered[1:]: parties[v[8]] += int(v[10]) # Show the geographic seat allocation for seat in sorted(seats.keys()): print "%50s" % seat, seats[seat] # Show the party seat allocation for k,v in parties.iteritems(): if v > 100000: print "%30s %10d" % (k,v) # Other misc data seat_powers = [s[2] for s in seats.values()] print "Total geographic power:", sum(seat_powers) print "Average geographic seat power:", sum(seat_powers) / len(seat_powers)