Last active
July 28, 2016 10:04
-
-
Save tomchristie/f6573dde2fea8dcd3350521f81140089 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.data.parliament.uk/dataset/general-election-2015 | |
import tablib | |
coalition_parties = ('Lab', 'LD', 'Green') | |
factor = 1.0 | |
data = tablib.Dataset() | |
data.csv = open('../Desktop/hocl-ge2015-results-full.csv', 'r').read() | |
consituencies = {} | |
# {'Aberavon': {}, 'Aberconwy': {}, ...} | |
for line in data: | |
consituencies[line[2]] = {} | |
# {'Aberavon': {'Lab': 15416, 'UKIP': 4971, 'Con': 3742, ...}, ...} | |
for line in data: | |
consituencies[line[2]][line[8]] = int(line[14]) | |
# Print number of seats won by each party | |
seats = {} | |
for results in consituencies.values(): | |
party = max(results.items(), key=lambda x: x[1])[0] | |
if party in seats: | |
seats[party] += 1 | |
else: | |
seats[party] = 1 | |
sorted_items = sorted(seats.items(), key=lambda x: -x[1]) | |
for party, seats in sorted_items: | |
print "%8s: %3d" % (party, seats) | |
print '===' | |
# Remove individual coalition votes and add to highest in seat. | |
for results in consituencies.values(): | |
# Get the subset of results just for coalition parties. | |
subset = [ | |
item for item in results.items() | |
if item[0] in coalition_parties | |
] | |
# No coalition parties ran in seat, ignore. | |
if not subset: | |
continue | |
# Determine the highest ranked coalition party. | |
party = max(subset, key=lambda x: x[1])[0] | |
total = 0 | |
for individual in coalition_parties: | |
if individual != party and individual in results: | |
extra = results[individual] * factor | |
results[individual] -= extra | |
results[party] += extra | |
# Print number of seats won by each party | |
seats = {} | |
for results in consituencies.values(): | |
party = max(results.items(), key=lambda x: x[1])[0] | |
if party in seats: | |
seats[party] += 1 | |
else: | |
seats[party] = 1 | |
sorted_items = sorted(seats.items(), key=lambda x: -x[1]) | |
for party, seats in sorted_items: | |
print "%8s: %3d" % (party, seats) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python count.py | |
Con: 330 | |
Lab: 232 | |
SNP: 56 | |
LD: 8 | |
DUP: 8 | |
SF: 4 | |
SDLP: 3 | |
PC: 3 | |
UUP: 2 | |
UKIP: 1 | |
Ind: 1 | |
Spk: 1 | |
Green: 1 | |
=== | |
Con: 285 | |
Lab: 260 | |
SNP: 52 | |
LD: 29 | |
DUP: 8 | |
SF: 4 | |
SDLP: 3 | |
PC: 3 | |
UUP: 2 | |
UKIP: 1 | |
Ind: 1 | |
Spk: 1 | |
Green: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment