代码:
candidates_and_fp_votes = {'a': 25, 'b': 0, 'c': 17, 'd': 23, 'e': 0}
print(candidates_and_fp_votes)
final_vals = [a for a, b in candidates_and_fp_votes.items() if
list(candidates_and_fp_votes.values()).count(b) > 1]
print("Duplicates are: ")
for i in final_vals:
print(i)
print(final_vals)
if not final_vals:
print("No tiebreaker sys required")
else:
print("Tie-breaker required")
目前,输出“需要连接器”为“b”,“e”的值为0。 我想这样它只能找到影响最高或第二高值的重复项。 例如:
现在,它应该打印“不需要决胜局系统sys,因为即使有'b'和'e'的副本,它也不是 带有第二个和最高值'25'和'23'
如果dict为= {'a':25,'b':0,'c':23,'d':23,'e':0} 它会打印出“因为它影响第二个最高价值所需的破坏者 - 23。
另外,我只是稍微改变一下,如果最高得分与数字重复,但这个数字不会与第二个最高重复,那么它只会输出“无需断层连接系统”
我该怎么做?
提前致谢。
答案 0 :(得分:1)
如果candidates_and_fp_votes
中的第二个最高值出现平局,那么它会打印"需要打破平局"。
#candidates_and_fp_votes = {'a': 25, 'b': 0, 'c': 17, 'd': 23, 'e': 0}
candidates_and_fp_votes = {'a': 25, 'b': 0, 'c': 23, 'd': 23, 'e': 0}
print(candidates_and_fp_votes)
final_vals = [ b for a, b in candidates_and_fp_votes.items() if list(candidates_and_fp_votes.values()).count(b) > 1]
dup=sorted(set(final_vals), reverse=True)
act=sorted(set(list(candidates_and_fp_votes.values())), reverse=True)
print("duplicates: ", dup)
print("actual: ", act)
if dup:
if dup[0] == act[0]:
del act[0]
del dup[0]
if dup:
if dup[0] == act[0] or dup[0] == act[1]:
print("Tie breaker required")
else:
print("No tie breaker required")
else:
print("No tie breaker required")
答案 1 :(得分:1)
只需保留a
和b
即可选择:
candidates_and_fp_votes = {'a': 25, 'c': 25, 'b': 0, 'e': 23, 'd': 23}
print(candidates_and_fp_votes)
final_vals = [[a,b] for a, b in candidates_and_fp_votes.items() if
list(candidates_and_fp_votes.values()).count(b) > 1]
print("Duplicates are: ")
for i in final_vals:
print(i)
fAndS=sorted(candidates_and_fp_votes.values())[len(candidates_and_fp_votes)-2:len(candidates_and_fp_votes)]
maxVals=[[a,b] for a, b in candidates_and_fp_votes.items() if b==max(candidates_and_fp_votes.values())]
if final_vals:
print("There is a duplicate with")
print(final_vals)
if len(maxVals)==2:
print("2 highest scoring candidates are equal")
print(maxVals)
print("No tiebreaker sys required")
else:
final_vals = [ a for a,b in final_vals if b in fAndS]
if not final_vals:
print("It is not a duplicate with the second and highest values")
print(fAndS)
print("No tiebreaker sys required")
else:
print("There is a duplicate with the second and highest values")
print(fAndS)
print("Tie-breaker required")
输出1:
{'a': 25, 'c': 17, 'b': 0, 'e': 0, 'd': 23} Duplicates are: ['b', 0] ['e', 0] There is a duplicate with [['b', 0], ['e', 0]] It is not a duplicate with the second and highest values [23, 25] No tiebreaker sys required
输出2:
{'a': 25, 'c': 23, 'b': 0, 'e': 0, 'd': 23} Duplicates are: ['c', 23] ['b', 0] ['e', 0] ['d', 23] There is a duplicate with [['c', 23], ['b', 0], ['e', 0], ['d', 23]] There is a duplicate with the second and highest values [23, 25] Tie-breaker required
输出3:
{'a': 25, 'c': 25, 'b': 0, 'e': 23, 'd': 23} Duplicates are: ['a', 25] ['c', 25] ['e', 23] ['d', 23] There is a duplicate with [['a', 25], ['c', 25], ['e', 23], ['d', 23]] 2 highest scoring candidates are equal [['a', 25], ['c', 25]] No tiebreaker sys required
<强> [编辑] 强> 这是一个更简单的答案,只是将所有条件放在一起: (仅根据dict排序)
candidates_and_fp_votes = {'a': 25, 'c': 25, 'b': 0, 'e': 0, 'd': 23}
sorted_votes = sorted([[value,key] for key,value in candidates_and_fp_votes.items()])
resultList=[]
dictSize = len(candidates_and_fp_votes)
print sorted_votes
if(dictSize>=3):
if sorted_votes[dictSize-2][0]==sorted_votes[dictSize-3][0]:
print("Need tie break between")
resultList = [ key for value,key in sorted_votes if value==sorted_votes[dictSize-2][0] ]
print resultList
else:
print("Winers")
resultList = [key for value,key in sorted_votes[dictSize-2:dictSize]]
print resultList
输出:
[[0, 'b'], [0, 'e'], [23, 'd'], [25, 'a'], [25, 'c']] Winers ['a', 'c']
答案 2 :(得分:0)
要查找最大值或第二高的副本,您可以尝试:
candidates_and_fp_votes = {'a': 25, 'b': 0, 'c': 17, 'd': 23, 'e': 0}
tie_breakers = [a for a, b in candidates_and_fp_votes.items() if list(candidates_and_fp_votes.values()).count(b) > 1 and b in [d for c, d in sorted(candidates_and_fp_votes.items(), key=lambda x:x[-1])[-2:]]]
输出:
['c', 'd']
答案 3 :(得分:0)
您可以使用heapq.nlargest
查找三个最大值,然后检查它们:
import heapq
max3 = heapq.nlargest(3, candidates_and_fp_votes.values())
max3现在将按降序包含三个最大值(保留多次出现)。其余的应该很容易。
答案 4 :(得分:-1)
据我了解,您希望根据最高票数确定获胜者,以及是否需要打破平局。你可以这样做:
import operator
candidates_and_fp_votes = {'a': 25, 'b': 0, 'c': 17, 'd': 23, 'e': 0}
sorted_candidates = sorted(candidates_and_fp_votes.items(), key=operator.itemgetter(1), reverse=True)
sorted_candidates
的输出如下:
[('a', 25), ('d', 23), ('c', 17), ('b', 0), ('e', 0)]
然后你可以迭代这个:
tie = False
for idx, _ in enumerate(sorted_candidates):
candidate, votes = sorted_candidates[idx]
next_candidate, next_votes = sorted_candidates[idx+1]
if votes > next_votes:
if tie is True:
print("Tie Breaker needed!")
else:
print(f"Candidate {candidate} is the Winner!")
break
elif votes == next_votes:
print("Tie!")
tie = True
到达那里的众多方法之一。我希望有所帮助。
参考:
python sort dict on value
编辑:
你要确定是否是平局。我的方法是扭转这个词。将选票作为关键并创建具有相同票数的候选人名单。像这样:
from collections import defaultdict
reversed_dict = defaultdict(list)
for key,value in candidates_and_fp_votes.items():
reversed_dict[value].append(key)
对外输出:
defaultdict(list, {0: ['b', 'e'], 17: ['c'], 23: ['d'], 25: ['a']})
任何具有len&gt;候选值列表的键1需要打破平局。