[英]Dictionary Values Printing Out Unusual/Weird Numbers
我試圖解決關於 codechef 的問題( https://www.codechef.com/problems/UCL ),我很困惑為什么打印的數字完全不正確,我真的不知道這些數字是怎么來的成為。
我在這里有一個輸入文件:input.txt
2
manutd 8 vs. 2 arsenal
lyon 1 vs. 2 manutd
fcbarca 0 vs. 0 lyon
fcbarca 5 vs. 1 arsenal
manutd 3 vs. 1 fcbarca
arsenal 6 vs. 0 lyon
arsenal 0 vs. 0 manutd
manutd 4 vs. 2 lyon
arsenal 2 vs. 2 fcbarca
lyon 0 vs. 3 fcbarca
lyon 1 vs. 0 arsenal
fcbarca 0 vs. 1 manutd
a 3 vs. 0 b
a 0 vs. 0 c
a 0 vs. 0 d
b 0 vs. 0 a
b 4 vs. 0 c
b 0 vs. 0 d
c 0 vs. 0 a
c 0 vs. 0 b
c 1 vs. 0 d
d 3 vs. 0 a
d 0 vs. 0 b
d 0 vs. 0 c
這是我打印出奇怪數字的代碼:
fh = open('competitiveProgrammingInput.txt', "r")
tc = int(fh.readline())
for i in range(tc):
teamsList = {}
highest = 0
for i in range(12):
homeScore = 0
awayScore = 0
li = fh.readline().split()
home, homeG, awayG, away = li[0], int(li[1]), int(li[3]), li[4]
if homeG > awayG:
homeScore = 3
awayScore = 0
elif awayG > homeG:
awayScore = 3
homeScore = 0
elif awayG == homeG:
homeScore = 1
awayScore = 1
homeDifference = homeG - awayG
awayDifference = awayG - homeG
if home in teamsList:
homeDifference = homeDifference + teamsList[home][1]
homeScore += teamsList[home][0]
del teamsList[home]
newD = {home: [homeScore, homeDifference]}
teamsList[home] = [homeScore, homeDifference]
else:
teamsList[home] = [homeScore, homeDifference]
if away in teamsList:
awayDifference = awayDifference + teamsList[away][1]
awayScore = awayDifference + teamsList[away][0]
del teamsList[away]
newD = {away: [awayScore, awayDifference]}
teamsList[away] = [awayScore, awayDifference]
else:
teamsList[away] = [awayScore, awayDifference]
print(teamsList)
具體來說,是homeScore和awayScore值不起作用。
為了給你一些背景,這個問題是獲得足球錦標賽的排名,輸入的格式類似於主隊,主隊進球,'vs',客隊進球,客隊進球多的人得到+3分。 那就是 awayScore 和 homeScore 變量。
公布的數字與我能想到的任何事情都沒有關聯。
非常感謝你的幫助。
這里有幾點:
newD
? 你沒有在任何地方使用它。 至於您的實際問題,您正在使用awayDifference
更新awayScore
,因此該行可能應該如下所示:
awayScore += teamsList[away][0]
不過,我懷疑這不是您唯一的問題。 您在問題中說獲勝的球隊得分為+3,但如果比賽結果為平局,他們也會獲得+1。 此外,你的分差對輸的球隊來說是負數。 這些值是預期的嗎?
但是,您的代碼架構很奇怪,因為您使用這些值來修改其他值,然后覆蓋原始值。 要獲取代碼的“離開”分支,也許您應該像這樣嘗試:
if away in teamsList:
teamsList[away][1] += awayDifference
teamsList[away][0] += awayScore
else:
teamsList[away] = [awayScore, awayDifference]
然后你可以對“home”分支做同樣的事情。
但是,這不是最佳實踐,因為很容易混淆哪個索引對應於“分數”與“差異”。 事實上,這就是讓你在這里絆倒的原因。 相反,我會添加一個 class:
class TeamEntry:
def __init__(self, score, difference):
self.Score = score
self.Difference = difference
def Add(self, score, difference):
self.Score += score
self.Difference += difference
然后,您“離開”的分支將如下所示:
if away in teamsList:
teamsList[away].Add(awayScore, awayDifference)
else:
teamsList[away] = TeamEntry(awayScore, awayDifference)
這更清潔。
對原始代碼進行代碼修復
代碼
fh = open('competitiveProgrammingInput.txt', "r")
tc = int(fh.readline())
for i in range(tc):
teamsList = {}
highest = 0
for i in range(12):
homeScore = 0
awayScore = 0
li = fh.readline().split()
home, homeG, awayG, away = li[0], int(li[1]), int(li[3]), li[4]
if homeG > awayG:
homeScore = 3
awayScore = 0
elif awayG > homeG:
awayScore = 3
homeScore = 0
elif awayG == homeG:
homeScore = 1
awayScore = 1
homeDifference = homeG - awayG
awayDifference = awayG - homeG
if home in teamsList:
homeDifference += teamsList[home][1]
homeScore += teamsList[home][0]
teamsList[home] = [homeScore, homeDifference]
else:
teamsList[home] = [homeScore, homeDifference]
if away in teamsList:
awayDifference += teamsList[away][1]
awayScore += teamsList[away][0]
teamsList[away] = [awayScore, awayDifference]
else:
teamsList[away] = [awayScore, awayDifference]
# Sort by points then by goal difference
results = sorted(teamsList.items(), key=lambda kv: tuple(kv[1]), reverse=True)
# Print first two names of results (since sorted in descending order)
print(*[name for name, value in results[:2]])
Output
manutd fcbarca
d b
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.