我正在嘗試使用名爲「score.txt」的文本文件創建名稱和點的字典。 文件的內容stuctured這樣的:正在尋找提高的字典中的分數計算
Anders Johansson 1
Karin Johansson 1
Anders Johansson 2
Eva Johansson 0
+ 2000個名稱,並指出在總
我得到的分數,計算和尋找高得分的方法是這樣的
f = open('score.txt').read().split()
d = {}
for x in range(0, len(f), 3):
name, value = ' '.join([f[x], f[x+1]]), int(f[x+2])
if(name in d):
d[name] += value
else:
d[name] = value
print(max(d, key=d.get),d[max(d, key=d.get)])
我很新的Python,所以我正在尋找方法來提高我的代碼。
這個問題很明確。你在做什麼?性能或代碼風格的改進?恐怕你不會得到這個問題的「正確」答案。 –
查看[CodeReview](http://codereview.stackexchange.com/)。 – MSeifert
這可能是Code Review的話題,只要** A **代碼有效**和B **,它無論如何都不是假想的或不完整的。如果您選擇去那裏,請在發佈之前閱讀主題指南。 – Quill