I am trying to implement a ranked voting system in Python. I have this code:
import numpy as np
import itertools
candidates = ['Bob', 'Alice', 'Jim', 'Sarah', 'Paul', 'Jordan']
votes = np.matrix(
'1 2 5 3 4 6;' \
'1 2 3 4 5 6;' \
'5 1 2 4 3 6;' \
'6 2 1 3 4 5;' \
'4 3 2 1 5 7'
)
pairs = itertools.combinations(candidates, 2)
d = dict.fromkeys(pairs, 0)
for pair in pairs:
print(pair)
Dictionary:
d
=> {('Paul', 'Jordan'): 0, ('Alice', 'Sarah'): 0, ('Alice', 'Jim'): 0, ('Alice', 'Paul'): 0, ('Jim', 'Sarah'): 0, ('Sarah', 'Paul'): 0, ('Bob', 'Alice'): 0, ('Bob', 'Jordan'): 0, ('Jim', 'Jordan'): 0, ('Jim', 'Paul'): 0, ('Sarah', 'Jordan'): 0, ('Bob', 'Paul'): 0, ('Bob', 'Sarah'): 0, ('Bob', 'Jim'): 0, ('Alice', 'Jordan'): 0}
This is what I want. But it seems that it destroys a list of tuples pairs.
If I select a dictionary line, the output of the code is:
('Bob', 'Alice')
('Bob', 'Jim')
('Bob', 'Sarah')
('Bob', 'Paul')
('Bob', 'Jordan')
('Alice', 'Jim')
('Alice', 'Sarah')
('Alice', 'Paul')
('Alice', 'Jordan')
('Jim', 'Sarah')
('Jim', 'Paul')
('Jim', 'Jordan')
('Sarah', 'Paul')
('Sarah', 'Jordan')
('Paul', 'Jordan')
Nothing is printed on the dictionary string.
I also tried to understand the dictionary
d = {pair: 0 for pair in pairs}
And the same thing happened. Why is the list destroyed pairs?