Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 3.4中,如何对字典中每个键的值从高到低进行排序?_Python_Sorting_Dictionary - Fatal编程技术网

在python 3.4中,如何对字典中每个键的值从高到低进行排序?

在python 3.4中,如何对字典中每个键的值从高到低进行排序?,python,sorting,dictionary,Python,Sorting,Dictionary,目前我的代码如下所示: 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 lin

目前我的代码如下所示:

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]))
我必须根据每个学生的名字按字母顺序打印他们的最高分数


如何对每个
键中的值进行排序?

要对每个学生的分数进行排序,可以使用与对字典键进行排序相同的功能

假设您还想更新分数列表,一种可能的实现方式是:

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]))
如果没有排序,并且您不想对每个学生的分数列表进行排序,那么使用
max
函数就足够了,即使用

ordered_keys = sorted(score_dict.keys(), key=lambda k: max(score_dict[k]), reverse=True)
有关排序
功能的更多信息,请查看。

您不希望排序以打印每个学生的最高分数:

如果确实要将值从最高到最低排序,只需调用值上的sorted并使用reverse=True:

sorted(score_dict[key], reverse=True)

但是,你不需要为了得到最大值而对列表进行排序。

为什么你要在循环中反复设置
score\u dict[line[0]]=scores\u list
?@PadraicCunningham:你的意思是,为什么你要在循环中反复设置
score\u dict[line[0]=scores\u list
,这就是我希望我所推断的,枚举逻辑看起来也很奇怪。OP不是在尝试对键进行排序,而是在尝试对键进行排序values@PadraicCunningham你是对的,我第一次误解了这个问题。现在我相应地修正了答案。谢谢@alberto.quattrini。我怎样才能把每个学生的分数从高到低打印出来。例如:詹姆斯10号(新线)鲍勃6号(新线)查尔斯3@minions有关
排序
函数如何工作的更多详细信息,请参阅先前版本的答案。为了方便起见,我再次用代码更新了答案,该代码打印了一个学生列表,按照他们获得的最高分数排序,假设分数已经排序。使用此代码时,它会输出每个用户的姓名及其最高分数,但不会按顺序显示。输出为:詹姆斯10(新线)查尔斯6(新线)鲍勃8。它必须按照从最高值到最低值的顺序排列,因此我的预期输出是:James 10(新线)Bob 8(新线)Charles 6。如何修改此代码以获得此输出?然后制作一个配对列表,并使用第二个元素作为排序依据进行排序。尽管这不是根据问题要求的字母顺序
sorted(score_dict[key], reverse=True)