好吧,我试着写一种计算排名选票的方法,称为成对比较法。我应该明确指出,我是一个完全的新手,当它涉及到编写每一个字的意义上的代码。到目前为止,我已经成功地创建了选票(感谢这个网站上的另一个用户),并根据位置将它们分成两组。接下来,我需要将它们分成不同的列表,这样每个有序对中的候选者都在一起,而不考虑级别(这是确定候选人与其他人相比表现如何的一部分)。以下是我目前掌握的代码:
import itertools
import random
import collections
candidates = ['Trump', 'Cruz', 'Rubio', 'Carson', 'Fiorina']
def simul_ballots(num_voters):
ballots = []
choice = candidates[:]
for _ in range(num_voters):
random.shuffle(choice)
ballots.append(choice[:])
return ballots
n=3
ballots = simul_ballots(n)
i=0
m=0
oPairs = []
while i < n:
for a, b in itertools.combinations(ballots[i], 2):
x = (a,b)
x
ops = list(x)
oPairs.append(ops)
i += 1
oPairs
l = len(oPairs)-1
k=0
j=(k+1)
while (k < l):
print oPairs[k]
while (j < l):
#if all (x in oPairs[i] for x in oPairs[j]):
if (set(oPairs[k])==set(oPairs[j])):
print oPairs[j]
j+=1
k+=1
到目前为止,我还停留在最后一部分。我似乎不明白如何将每个子列表与其他子列表进行比较(没有重复,重要的是子列表的数量与我开始时相同。在这个例子中,为了测试的目的,我只生成了3组投票,因此应该有三个有序的对使用相同的候选者,而不考虑位置(我将需要稍后的定位来为候选对象评分)。如有任何正确的建议或建议,我们将不胜感激!在
我真的不知道你想用你的代码做什么。在
既然您已经导入了}是一个很好的工具来汇总成对比较的结果。我会用以下方法对候选对象进行迭代:
collections
,那么您似乎知道{然后,在每个选票上重复:
^{pr2}$如果}得一分。可通过以下方式进行检查:
^{3}$can1
第一个出现在选票中,他们得一分,否则{计票后(仅此一对),如果}赢得更多选票,他们得1分,否则他们都得半分。在
can1
赢得更多选票,他们得一分,否则如果{把这些放在一起可能看起来像:
相关问题 更多 >
编程相关推荐