Advertisement
robjones90

Alleviate candidates 2

Oct 6th, 2022
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import collections
  2. import functools
  3. import operator
  4.  
  5. #dictionary of data
  6. candidates = [
  7.          {'Joshua': 192,
  8.          'Aldrin': 48,
  9.          'Dianna': 206,
  10.          'Donna': 37,
  11.          'David': 195
  12.          },
  13.          {'Joshua': 200,
  14.          'Aldrin': 200,
  15.          'Dianna': 150,
  16.          'Donna': 67,
  17.          'David': 123
  18.          },
  19.          {'Joshua': 200,
  20.          'Aldrin': 200,
  21.          'Dianna': 150,
  22.          'Donna': 67,
  23.          'David': 123
  24.          },
  25.          {'Joshua': 200,
  26.          'Aldrin': 200,
  27.          'Dianna': 150,
  28.          'Donna': 67,
  29.          'David': 123
  30.          }]
  31.          
  32. print('Total number of columns =%s' %len(candidates) )
  33.  
  34.          
  35. def count_votes(candidate_dictionary):
  36.     # sum the values with same keys
  37.     result= dict(functools.reduce(operator.add,
  38.       map(collections.Counter, candidate_dictionary)))
  39.     return result
  40. print(count_votes(candidates))
  41.  
  42. totals = count_votes(candidates)
  43.  
  44. total_votes = 0
  45. for person, votes in totals.items():
  46.     total_votes += votes
  47.    
  48. for person, votes in totals.items():
  49.     rounded = round(votes / total_votes * 100)
  50.     print(f"Candidate {person} got {rounded}% of vote")
  51.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement