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 如何将多位数字典值从最高到最低排序_Python_Sorting_Dictionary - Fatal编程技术网

Python 如何将多位数字典值从最高到最低排序

Python 如何将多位数字典值从最高到最低排序,python,sorting,dictionary,Python,Sorting,Dictionary,有没有办法用多个数字对字典值进行排序?我尝试过这里使用的方法:,但它只适用于单位数的整数值。例如,我的代码当前如下所示: for line in students: student = line.split(' ') d[student[1]] = (student[3]) print (d) od = sorted(d.items(), key=lambda x: x[1], reverse=True) print(od)

有没有办法用多个数字对字典值进行排序?我尝试过这里使用的方法:,但它只适用于单位数的整数值。例如,我的代码当前如下所示:

for line in students:
        student = line.split(' ')
        d[student[1]] = (student[3])
        print (d)
     od = sorted(d.items(), key=lambda x: x[1], reverse=True)
     print(od)
其中students是该文本文件的.readlines()结果,我使用的是第一个分数:

Name= kirsty Score= 9 10 10 `
Name= anne Score= 4 5 6  
Name= charlie Score= 1 1 1 
Name= bruce Score= 7 8 9 
Name= danny Score= 10 11 12 
Danny应该是字典中的第一个条目,但我的实际输出是:

[('kirsty', '9'), ('bruce', '7'), ('anne', '4'), ('danny', '10'), ('charlie', '1')]

有人能帮忙吗?

更改键函数,将排序计算的值转换为数字。假设只使用整数,则排序行为:


od=sorted(d.items(),key=lambda x:int(x[1]),reverse=True)

更改键函数,将计算出的排序值转换为数字。假设只使用整数,则排序行为:


od=sorted(d.items(),key=lambda x:int(x[1]),reverse=True)
您需要转换为
int
s进行比较。您看到的行为来自比较字符串

e、 g-


您需要转换为
int
s进行比较。您看到的行为来自比较字符串

e、 g-


尝试将lambda x:int(x[1])作为排序键!尝试将lambda x:int(x[1])作为排序键!非常感谢。这正是我所需要的——对不起,我回答得这么晚!非常感谢。这正是我所需要的——对不起,我回答得这么晚!
sorted(d.items(), key = lambda x: int(x[1]), reverse  = True)