Advertisement
Guest User

Untitled

a guest
Nov 10th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import json
  2.  
  3.  
  4. with open("C:\\Users\\Li\Downloads\\president MI.json", "r", encoding="utf8") as read_file:
  5.     x = json.load(read_file)
  6.     data = x["data"]["races"][0]["timeseries"]
  7.     #print(data)
  8.  
  9.     last_votes = 0
  10.     reduced_total = 0
  11.     for i in data:
  12.         if i['votes'] < last_votes:
  13.             print(i['votes'] - last_votes)
  14.             reduced_total = reduced_total + (i['votes'] - last_votes)
  15.         last_votes = i['votes']
  16.     print("total votes reduced: ", reduced_total)
  17.  
  18.     candidate = "trumpd"
  19.     reduced_count = 0
  20.     reduced_total = 0
  21.     last_data = data[0]
  22.     for i in data[1:]:
  23.         votes = i['votes'] * i['vote_shares'][candidate]
  24.         last_votes = last_data['votes'] * last_data['vote_shares'][candidate]
  25.         if votes < last_votes:
  26.             reduced_count = reduced_count + 1
  27.             print(votes - last_votes)
  28.             reduced_total += (votes - last_votes)
  29.         last_data = i
  30.     print(reduced_count)
  31.     print("total votes taken from", candidate, ":", reduced_total)
  32.  
  33.     candidate = "bidenj"
  34.     reduced_count = 0
  35.     reduced_total = 0
  36.     last_data = data[0]
  37.     for i in data[1:]:
  38.         votes = i['votes'] * i['vote_shares'][candidate]
  39.         last_votes = last_data['votes'] * last_data['vote_shares'][candidate]
  40.         if votes < last_votes:
  41.             reduced_count = reduced_count + 1
  42.             print(votes - last_votes)
  43.             reduced_total += (votes - last_votes)
  44.         last_data = i
  45.     print(reduced_count)
  46.     print("total votes taken from", candidate, ":", reduced_total)
  47.  
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement