[英]How to aggregate the scores from different rows and return the maximum total scores in Python?
[英]Picking the maximum scores from a python 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))
你的代碼確實消除了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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.