你循環做兩件事情。首先,它向names
列表(排序分數的另一個列表以及單個字符串)附加一些新值。然後它打印列表的內容,用換行符分隔。這重複列表中的幾個項目,你可以在你的輸出看(簡寫,因爲我不能在所有的列表內容懶得打字):
Korbin # these first two lines are from the first iteration of the loop
[ ... ] # 1
Korbin # the next four are from the second iteration
[ ... ] # 2
Bob # 2
[ ... ] # 2
Korbin # The next six are from the third iteration
[ ... ] # 3
Bob # 3
[ ... ] # 3
Dylan # 3
[ ... ] # 3
Korbin # The next eight (which I'll not show all of) are from the fourth iteration
[ ... ] # 4
# etc.
你的問題表明,這是不是你想要的。我懷疑你應該不打擾列表,並且只是在循環中直接使用print
名稱和其他統計列表,或者你不應該在循環中打印,並且最後只使用一個單獨的print
調用。
這裏的,只是不直接印刷在環,與你行解析固定了較小的問題一起實現:
info = open("resultsA.txt", "r")
for line in info:
x = line.strip().split(", ") # fix up the list contents
print(x[0])
scores = x[1:4] # don't concatenate the strings, but slice the list
ascending = sorted(scores)
print(ascending)
'ascending'是一個列表。你可能想使用'names.extend(ascending)'(儘管如你所見,它包含一些不應該打印IMO的換行符和空格) –
可能你也想'scores = x [1], x [2],x [3]' –
非常感謝。與逗號和\ n的問題進行了排序,但我不明白爲什麼它再次打印相同的名稱和分數。 – Nightly