擅长:python、mysql、java
<p>我建议做一本字典:</p>
<pre><code># Open Text File
file = input("Enter the name of the score file: ")
print("Contestant score:")
open_file = open(file, "r")
lines = open_file.readlines()
# Convert into one single line for ease
mystr = '\t'.join([line.strip() for line in lines])
# Split it and make dictionary
out = mystr.split()
entries = dict([(x, y) for x, y in zip(out[::2], out[1::2])])
# Unsorted Display
print(entries)
# Sorted Display
print(sorted(entries.items(), key=lambda s: s[0]))
</code></pre>
<p>输出:</p>
<pre><code>[('james', '3'), ('matt', '9'), ('peter', '5'), ('sophia', '5')]
</code></pre>
<p>您可以以任何形式显示/保存此词典,如CSV、JSONO或仅以这种方式保存</p>