从python列表中选择最大分数

时间:2018-09-19 09:29:21

标签: python python-3.x list

我已经开始编写代码,在该代码中我要进行不同回合的比赛,并且每回合必须消除最差的两对。

我目前处于第2轮,我正在让用户输入自己的评判结果,但无法从用户输入的分数列表中删除最差的两对。

这是我到目前为止所拥有的:

cA2_judge1 = int(input("score couple A out of 10"))
cA2_judge2 = int(input("score couple A out of 10"))
cA2_judge3 = int(input("score couple A out of 10"))
cA2_judge4 = int(input("score couple A out of 10"))
cA2_judge5 = int(input("score couple A out of 10"))
print("")
cC2_judge1 = int(input("score couple C out of 10"))
cC2_judge2 = int(input("score couple C out of 10"))
cC2_judge3 = int(input("score couple C out of 10"))
cC2_judge4 = int(input("score couple C out of 10"))
cC2_judge5 = int(input("score couple C out of 10"))
print("")
cD2_judge1 = int(input("score couple D out of 10"))
cD2_judge2 = int(input("score couple D out of 10"))
cD2_judge3 = int(input("score couple D out of 10"))
cD2_judge4 = int(input("score couple D out of 10"))
cD2_judge5 = int(input("score couple D out of 10"))
print("")
cF2_judge1 = int(input("score couple F out of 10"))
cF2_judge2 = int(input("score couple F out of 10"))
cF2_judge3 = int(input("score couple F out of 10"))
cF2_judge4 = int(input("score couple F out of 10"))
cF2_judge5 = int(input("score couple F out of 10"))


listA2 = [cA2_judge1, cA2_judge2, cA2_judge3, cA2_judge4, 
cA2_judge5]
listA2.remove(min(listA2))
listA2.remove(max(listA2))
scoresA2=listA2
print("-------------------------")
print(".                       .")
print("Couple A scored",scoresA2)
print("This makes their total", sum(scoresA2))

listC2 = [cC2_judge1, cC2_judge2, cC2_judge3, cC2_judge4, 
cC2_judge5]
listC2.remove(min(listC2))
listC2.remove(max(listC2))
scoresC2=listC2
print("-------------------------")
print(".                       .")
print("Couple C scored",scoresC2)
print("This makes their total", sum(scoresC2))

listD2 = [cD2_judge1, cD2_judge2, cD2_judge3, cD2_judge4, 
cD2_judge5]
listD2.remove(min(listD2))
listD2.remove(max(listD2))
scoresD2=listD2
print("-------------------------")
print(".                       .")
print("Couple D scored",scoresD2)
print("This makes their total", sum(scoresD2))

listF2 = [cF2_judge1, cF2_judge2, cF2_judge3, cF2_judge4, 
cF2_judge5]
listF2.remove(min(listF2))
listF2.remove(max(listF2))
scoresF2=listF2
print("-------------------------")
print(".                       .")
print("Couple F scored",scoresF2)
print("This makes their total", sum(scoresF2))

listR2 = [sum(scoresA2), sum(scoresC2), sum(scoresD2), 
sum(scoresF2)]
listR2.remove(min(listR2))
listR2.remove(min(listR2))
print("")
print("")
print("This leaves us with the highest score of",max(listR2))

1 个答案:

答案 0 :(得分:3)

您的代码确实消除了2个最差的得分,但失去了对夫妇的参考。您应该使用成对的列表(couple, score),根据得分对其进行排序,然后删除最低的2对。这样,您就可以显示剩下的夫妻。

您可以使用:

listR2 = [('A', sum(scoresA2)), ('C', sum(scoresC2)), ('D', sum(scoresD2)), 
('F', sum(scoresF2))]
listR2 = sorted(listR2, key = lambda x: x[1], reverse = True)[:-2]
print("Remaining couples are",
    " and ".join(("{0} with a total score of {1}".format(*i) for i in listR2)))