Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 排序从高到低列出并定义变量_Python_List_Sorting - Fatal编程技术网

Python 排序从高到低列出并定义变量

Python 排序从高到低列出并定义变量,python,list,sorting,Python,List,Sorting,这是我的代码,我如何在最后更新name_分数,以便我可以稍后调用它(使用全局)并对其进行排序 with open(class_number, "r") as file: #1 file.seek(0) #2 scores = file.readlines() #3 name_scores = { } #4 for line in scores: #5 name, score = line.rstrip('\n').split(':') #6

这是我的代码,我如何在最后更新name_分数,以便我可以稍后调用它(使用全局)并对其进行排序

with open(class_number, "r") as file: #1
    file.seek(0) #2
    scores = file.readlines() #3
    name_scores = { } #4
    for line in scores: #5
        name, score = line.rstrip('\n').split(':') #6
        score = int(score) #7
        if name not in name_scores: #8
            name_scores[name] = []   #9    # Initialize score list
        name_scores[name].append(score) #10  # Add the most recent score
        if len(name_scores[name]) > 3:   #11
            name_scores[name].pop(0)   #12 
当我尝试在函数外部打印(姓名和分数)时 我得到:

但我不知道我想打印的是不是“姓名分数”

我试着按字母顺序从高到低排列最后三个分数

with open(view_class, "r") as file: #1
    for name in sorted(name_scores): #16
        name_scores = (sort(name_scores, reverse = True))
        print(name, '-', name_scores[name])

class\u分数
还是
name\u分数
?打印
print
语句在哪里?@PeterWood name\u分数,我没有在任何地方使用class\u分数为什么?你说:我尝试打印(“class\u分数”)对不起,让我附加一秒钟
with open(view_class, "r") as file: #1
    for name in sorted(name_scores): #16
        name_scores = (sort(name_scores, reverse = True))
        print(name, '-', name_scores[name])