私のコードは次のようになります。私は自分の名前のアルファベット順に各学生の最高のスコアを印刷する必要がありpython 3.4の各キーについて、辞書の値を高い順に並べ替えるにはどうすればいいですか?現時点で
import csv #this imports the CSV module, which enables us to read the file easier than using file.readlines()
score_dict = {} #this creates an empty dictionary
class_file = open('Class-A.txt','r') #this opens the file and reads the lines inside
scores = csv.reader(class_file) #this stores the class_file data as a readable object (that can be stripped even though it is a list) into the variable scores
for line in scores: #this loops through the different scores in each line
if line: #this ignores and empty rows of text in the file
scores_list = [] #this creates an empty array that will contain the list of scores for each student
for key, column in enumerate(line):
if key != 0: #this ignores the first column of text in the file as that will be used as the key
scores_list.append(int(column.strip())) #this appends the array to containing scores that have been stripped of whitespace and newlines. It also converts the scores into integers because in the text file, the scores are strings.
score_dict[line[0]] = scores_list #this inserts the list of scores into the dictionary
exit
for key in sorted(score_dict):
print ("%s: %s" % (key, score_dict[key]))
。
key
の値を並べ替えるにはどうすればよいですか?
ループ内で 'score_dict [line [0]] = scores_list'を何度も繰り返し設定していますか? –
@PadraicCunningham:あなたはどういうわけか_なぜあなたはループ内でscore_dict [line [0] = scores_list'を何度も何度も何度も何度も何度も何度も繰り返していますか?_ – zondo
可能な重複[値でPython辞書をソート] /stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – pixis