Skip to content

Instantly share code, notes, and snippets.

@hoffrocket
Last active November 3, 2020 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffrocket/a3d382dc2eccad16861d737fe30b2195 to your computer and use it in GitHub Desktop.
Save hoffrocket/a3d382dc2eccad16861d737fe30b2195 to your computer and use it in GitHub Desktop.
538 Election predictions to TSV
from urllib import request
import json
all_votes = json.load(request.urlopen("https://projects.fivethirtyeight.com/2020-election-forecast/states_electoral_votes.json"))
predictions = json.load(request.urlopen("https://projects.fivethirtyeight.com/2020-election-forecast/state_summary.json"))[0]["states"]
predictions.sort(key=lambda p: p["state"])
for prediction in predictions:
state = prediction["state"]
vote_count = next(v["votes"] for v in all_votes if v["state"] == state)
# not an endorsement!
trump_prob = next(c["winprob"]/100 for c in prediction["candidates"] if c["candidate"] == "Trump")
print(f"{state}\t{vote_count}\t{trump_prob}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment