Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- import functools
- import operator
- #dictionary of data
- candidates = [
- {'Joshua': 192,
- 'Aldrin': 48,
- 'Dianna': 206,
- 'Donna': 37,
- 'David': 195
- },
- {'Joshua': 200,
- 'Aldrin': 200,
- 'Dianna': 150,
- 'Donna': 67,
- 'David': 123
- },
- {'Joshua': 200,
- 'Aldrin': 200,
- 'Dianna': 150,
- 'Donna': 67,
- 'David': 123
- },
- {'Joshua': 200,
- 'Aldrin': 200,
- 'Dianna': 150,
- 'Donna': 67,
- 'David': 123
- }]
- print('Total number of columns =%s' %len(candidates) )
- def count_votes(candidate_dictionary):
- # sum the values with same keys
- result= dict(functools.reduce(operator.add,
- map(collections.Counter, candidate_dictionary)))
- return result
- print(count_votes(candidates))
- totals = count_votes(candidates)
- total_votes = 0
- for person, votes in totals.items():
- total_votes += votes
- for person, votes in totals.items():
- rounded = round(votes / total_votes * 100)
- print(f"Candidate {person} got {rounded}% of vote")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement