I have been given this as a list of lists each containing either a number 1 2 3 and 0 (0 is repeated twice). Depending on the number and the position I would like a corresponding variable to get added 1 time for each occurrence.
ballots = [['1', '2', '3', '0', '0'],
['1', '3', '0', '2', '0'],
['1', '2', '3', '0', '0'],
['0', '3', '2', '0', '1'],
['1', '3', '0', '2', '0'],
['2', '0', '3', '1', '0'],
['0', '0', '2', '1', '3'],
['0', '1', '2', '3', '0'],
['0', '1', '0', '2', '3'],
['2', '3', '1', '0', '0'],
['3', '2', '0', '0', '1'],
['0', '1', '3', '2', '0'],
['0', '0', '1', '2', '3'],
['0', '0', '3', '2', '1'],
['1', '2', '3', '0', '0'],
['2', '1', '3', '0', '0'],
['0', '3', '2', '1', '0'],
['0', '2', '3', '0', '1'],
['1', '2', '3', '0', '0'],
['1', '0', '0', '3', '2'],
['2', '1', '3', '0', '0'],
['3', '1', '2', '0', '0'],
['2', '3', '0', '1', '0'],
['0', '0', '3', '1', '2'],
['0', '3', '1', '0', '2'],
['2', '1', '0', '0', '3'],
['2', '0', '0', '1', '3'],
['2', '0', '0', '1', '3'],
['3', '0', '1', '0', '2']]
For example, for the first list:
- the 1 in position 1 would mean that
candidate1vote1 += 1
- the 2 in the 2nd position would mean that
candidate2vote2 += 1
- the 3 in the 3rd position would mean that
candidate3vote3 += 1
All 0's are ignored but still counted as a space. For the second list:
- the 1 in the first position would mean that
candidate1vote1 += 1
- the 3 in the 2nd position would mean that
candidate3vote2 += 1
- the 2 in the 4th position would mean that
candidate4vote2 += 1
Basically the position corresponds to candidate1/2/3/4/5 and the value corresponds to either a 1st preference vote, 2nd preference vote or a 3rd preference vote.
Does anyone know how I'd be able to sort through the lists using for/while loops so that it goes through each ballot and each individual vote doing the corresponding sum?