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 如何使用lambda在字典中按元组值排序?_Python_Sorting_Dictionary - Fatal编程技术网

Python 如何使用lambda在字典中按元组值排序?

Python 如何使用lambda在字典中按元组值排序?,python,sorting,dictionary,Python,Sorting,Dictionary,如何在字典中按元组的值排序 我用lambda排序 我试着按名字的字母顺序和等级分类。 所以我想分别给出两个结果 scale = {'A+': 4.3, 'A': 4.0, 'A-': 3.7, 'B+': 3.3, 'B': 3.0, 'B-': 2.7, 'C+': 2.3, 'C': 2.0, 'C-': 1.7, 'D+': 1.3, 'D': 1.0, 'D-': 0.7, 'F' : 0.0} subject = {'math': {('Tom', 'B'

如何在字典中按元组的值排序

我用lambda排序 我试着按名字的字母顺序和等级分类。 所以我想分别给出两个结果

scale = {'A+': 4.3, 'A': 4.0, 'A-': 3.7,
   'B+': 3.3, 'B': 3.0, 'B-': 2.7,
   'C+': 2.3, 'C': 2.0, 'C-': 1.7,
   'D+': 1.3, 'D': 1.0, 'D-': 0.7,
   'F' : 0.0}


subject = {'math':  {('Tom', 'B'), ('Kevin','D')},
   'History':  {('Kate', 'A+'),('Eric','C'),('Hannah','F')}, 
   'English':  {('Eli', 'B-')}}`

from collections import defaultdict
def pass(subject, mingrade):
    output = defaultdict(list)
    for subject_name,subject_grade in subject.items():
        for student,grade in subject_grade:
            if scale[grade]>=mingrade:
                output[subject_name]+=[student]
    return output
我已经厌倦了按等级排序的代码,但它得到了TypeError:“set”对象不支持索引
对于排序中的k,v(db.items(),key=lambda x:x[1][1]):
然而,
对于排序中的k,v(db.items(),key=lambda x:x[1])

不会更改任何内容,但不会出现错误。

如果我理解正确,我认为您正在查找以下内容:

students, ranking = {}, {}
for s, grades in subject.items():
    ranking[s] = [n for n, g in sorted(grades, key=lambda x: scale[x[1]], reverse=True))]
    students[s] = [n for n, g in sorted(grades, key=lambda x: x[0]))]

如果我理解正确,我认为您正在寻找以下信息:

students, ranking = {}, {}
for s, grades in subject.items():
    ranking[s] = [n for n, g in sorted(grades, key=lambda x: scale[x[1]], reverse=True))]
    students[s] = [n for n, g in sorted(grades, key=lambda x: x[0]))]

我很确定我在你的另一个问题上留下了相同的评论:为什么
subject
元组集的值本身不是
dict
s<代码>主题={'math':{'Tom':'B','Kevin':'D'},…}。转换很简单:
subject={k:dict(v)表示k,v在subject.items()}
中。请回答您的问题并添加您试图使用的代码(以其他人可以运行的方式)。我必须保持这种方式。。如果我在for循环前面加上'subject={k:dict(v)for k,v在subject.items()中,它会得到ValueError:太多的值需要解包(预期为2)。我很确定我在你的另一个问题上留下了相同的评论:
subject
元组的值为什么在它们自己的右边不是
dict
s<代码>主题={'math':{'Tom':'B','Kevin':'D'},…}。转换很简单:
subject={k:dict(v)表示k,v在subject.items()}
中。请回答您的问题并添加您试图使用的代码(以其他人可以运行的方式)。我必须保持这种方式。。如果我在for循环前面加上'subject={k:dict(v)for k,v在subject.items()中,它会得到ValueError:太多的值无法解包(预期为2),您能编写完整的代码吗?我不知道应该把代码放在哪里,因为我需要另一个条件,这将很困难,因为问题中的代码没有显示错误的来源。请更新问题。您能将字典变为变量而不是打印它们吗<代码>对于s,subject.items()中的分数:我想要像'subject=(s,sorted(grades,key=lambda x:scale[x[1]],reverse=True))`这样我就可以再次使用字典,因为字典或者集合没有排序,你是说一个列表吗?是的,只要我的输出看起来像{'math':['Tom and'Kevin']}你能写完整的代码吗?我不知道应该把代码放在哪里,因为我需要另一个条件,这将很困难,因为问题中的代码没有显示错误的来源。请更新问题。您能将字典变为变量而不是打印它们吗<代码>对于s,subject.items()中的分数:我想要像'subject=(s,排序(grades,key=lambda x:scale[x[1]],reverse=True))`这样我就可以再次使用字典,因为字典或集合没有排序,你是说列表吗?是的,只要我的输出看起来像{'math':['Tom','Kevin']}