2016-03-18 10 views
-1

私のコードは次のようになります。私は自分の名前のアルファベット順に各学生の最高のスコアを印刷する必要があり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の値を並べ替えるにはどうすればよいですか?

+0

ループ内で 'score_dict [line [0]] = scores_list'を何度も繰り返し設定していますか? –

+0

@PadraicCunningham:あなたはどういうわけか_なぜあなたはループ内でscore_dict [line [0] = scores_list'を何度も何度も何度も何度も何度も何度も繰り返していますか?_ – zondo

+0

可能な重複[値でPython辞書をソート] /stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – pixis

答えて

1

各生徒のスコアをソートするために、辞書キーのソートに使用したのと同じ機能を使用できます。

あなたもスコアのリストを更新したいと仮定すると、可能な実装は次のようになります。あなたが辞書を移入されたときのソートも行うことができること

for key in sorted(score_dict): 
    # sorting the values. 
    score_dict[key] = sorted(score_dict[key], reverse=True) 
    # print of the highest score. 
    print ("%s: %s" % (key, score_dict[key][0])) 

注意。

ここでコメントでOPによって要求されたようOP

の要求に応じて更新し、以前の私の解釈が編集された彼らの最高得点(順学生のリストを印刷することができますコードの一部回答)。各生徒のスコアのリストは既に注文されているものとします。

ordered_keys = sorted(score_dict.keys(), key=lambda k: score_dict[k][0], reverse=True) 
for key in ordered_keys: 
    print("%s: %s" % (key, score_dict[key][0])) 

ご注文いただいていない、あなたは生徒ごとにスコアのリストを注文したくない場合は、すなわちsortedの詳細については

ordered_keys = sorted(score_dict.keys(), key=lambda k: max(score_dict[k]), reverse=True) 

を使用し、max機能を使用することで十分です機能については、https://wiki.python.org/moin/HowTo/Sorting#Key_Functionsをご覧ください。あなたは最高から最低までの値をソートしたいなかった場合

for key in sorted(score_dict): 
    print (key, max(score_dict[key])) 

、あなたは、単に=の値でソートし、逆を使用して呼び出します:あなたはmax印刷にソートされていない各学生の最高を獲得したい

+0

OPはキーをソートしようとしていません、値をソートしようとしています –

+0

@PadraicCunninghamあなたは正しいです、私は最初の質問を誤解しました。今私はそれに応じて答えを修正しました。 – albertoql

+0

ありがとうございます@ alberto.quattrinili。各生徒の得点を最高から最低までどのように印刷することができますか?例:James 10(改行)Bob 6(改行)Charles 3 – Minions

0

True:

ただし、最大値を取得するために並べ替える必要はありません。

+0

このコードを使用すると、最高のスコアで各ユーザーの名前が出力されますが、順番に表示されません。ジェームズ10(改行)ボブ8(改行)ボブ8(改行)ボブ8(改行)ボブ8 Charles 6.この出力を得るためにこのコードを変更するにはどうすればいいですか? – Minions

+0

次に、ペアのリストを作成し、2番目の要素をソートするキーとしてソートします。質問の要件に応じてアルファベット順ではありません –

関連する問題