I am currently working on a small project that involves analysing ballot papers and determining the winning candidate based on the number of votes they receive. The voting system used is very unusual. For example, if there are four candidates the vote could look like this: 3241 (3 points for the first candidate, 2 points for the second candidate etc). To determine the winner I have to add up all of the votes for each candidate. For example, if the 3 ballots were [1342, 3241, 3214] the amount of points for the first candidate would be 1+3+3 = 7 (Lower the better). Declaring the winner if there is no draw is very simple. It begins to get quite difficult when there is a tie for points. For the example above, the final score would be [7,7,9,7]. To declare the winner if there is a tie I need to look at the preference order of the votes (basically check how many 1's each candidate got, how many 2's each candidate got etc) and store them in a list which look something like this:
[[1, 0, 1, 1], [0, 2, 0, 1], [2, 1, 0, 0], [0, 0, 2, 1]]
I have managed to create the list above. However, I am now having trouble declaring the winner, in this situation the winner should be the 4th candidate. As the they are tied for first preference votes, but they have the most second preference votes out of the candidates who were initially tied.
Code:
for lists in drawList: # drawList is list of zeros
for num in lists:
if num == 0:
lists.remove(0)
maximum = max(lists)
count = lists.count(maximum)
if count == 1:
for i, num in enumerate(lists):
if num == count:
winner = nameList[i]
print(i)
break
I have tried the code above however it seems to give back a reversed result, declaring the loser as the winner. I am just wondering if there is a good way to declare a winner using the list above, comparing the number of votes each candidate got in each section. Thanks in advance.