我需要按列表中的第二个值对该字典进行排序。第一个字典“键值”对的值为341
。
{'Ariana Greenblatt': ['Young Gamora', 341], 'Stan Lee': ['Schoolbus Driver',
335], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Chadwick Boseman':
["T'Challa / Black Panther", 46], 'Letitia Wright': ['Shuri', 90], 'Ameenah
Kaplan': ["Gamora's Mother", 342], 'Florence Kasumba': ['Ayo', 114], 'Carrie
Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury
(uncredited)', 332], 'Kenneth Branagh': ['Asgardian Distress Call (voice)
(uncredited)', 357], 'Chris Evans': ['Steve Rogers / Captain America', 2],
'Chris Hemsworth': ['Thor Odinson', 6], 'Tom Vaughan-Lawlor': ['Ebony Maw',
97], 'Robert Pralgo': ['Thanos Reader', 344]}
在一些堆栈溢出问题中,我发现了OrderedDict
。可以用于此目的吗?但是我无法做到这一点。
PS:我想将此排序后的字典作为字典,而不是元组。
答案 0 :(得分:5)
在Python 3.6及更高版本中,命令是排序的,因此您只需使用自定义键即可对命令项进行排序:
d = {'Ariana Greenblatt': ['Young Gamora', 341], 'Stan Lee': ['Schoolbus Driver', 335], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Chadwick Boseman': ["T'Challa / Black Panther", 46], 'Letitia Wright': ['Shuri', 90], 'Ameenah Kaplan': ["Gamora's Mother", 342], 'Florence Kasumba': ['Ayo', 114], 'Carrie Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury (uncredited)', 332], 'Kenneth Branagh': ['Asgardian Distress Call (voice) (uncredited)', 357], 'Chris Evans': ['Steve Rogers / Captain America', 2], 'Chris Hemsworth': ['Thor Odinson', 6], 'Tom Vaughan-Lawlor': ['Ebony Maw', 97], 'Robert Pralgo': ['Thanos Reader', 344]}
print(dict(sorted(d.items(), key=lambda x: x[1][1])))
这将输出:
{'Chris Evans': ['Steve Rogers / Captain America', 2], 'Chris Hemsworth': ['Thor Odinson', 6], 'Chadwick Boseman': ["T'Challa / Black Panther", 46], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Letitia Wright': ['Shuri', 90], 'Tom Vaughan-Lawlor': ['Ebony Maw', 97], 'Florence Kasumba': ['Ayo', 114], 'Carrie Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury (uncredited)', 332], 'Stan Lee': ['Schoolbus Driver', 335], 'Ariana Greenblatt': ['Young Gamora', 341], 'Ameenah Kaplan': ["Gamora's Mother", 342], 'Robert Pralgo': ['Thanos Reader', 344], 'Kenneth Branagh': ['Asgardian Distress Call (voice) (uncredited)', 357]}
或者在Python 3.6之前,您可以改为使用collections.OrderedDict
:
from collections import OrderedDict
d = OrderedDict([('Ariana Greenblatt', ['Young Gamora', 341]), ('Stan Lee', ['Schoolbus Driver', 335]), ('Anthony Mackie', ['Sam Wilson / Falcon', 70]), ('Chadwick Boseman', ["T'Challa / Black Panther", 46]), ('Letitia Wright', ['Shuri', 90]), ('Ameenah Kaplan', ["Gamora's Mother", 342]), ('Florence Kasumba', ['Ayo', 114]), ('Carrie Coon', ['Proxima Midnight (voice)', 329]), ('Samuel L. Jackson', ['Nick Fury (uncredited)', 332]), ('Kenneth Branagh', ['Asgardian Distress Call (voice) (uncredited)', 357]), ('Chris Evans', ['Steve Rogers / Captain America', 2]), ('Chris Hemsworth', ['Thor Odinson', 6]), ('Tom Vaughan-Lawlor', ['Ebony Maw', 97]), ('Robert Pralgo', ['Thanos Reader', 344])])
print(OrderedDict(sorted(d.items(), key=lambda x: x[1][1])))
答案 1 :(得分:0)
或按def
排序:
d = {'Ariana Greenblatt': ['Young Gamora', 341], 'Stan Lee': ['Schoolbus Driver', 335], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Chadwick Boseman': ["T'Challa / Black Panther", 46], 'Letitia Wright': ['Shuri', 90], 'Ameenah Kaplan': ["Gamora's Mother", 342], 'Florence Kasumba': ['Ayo', 114], 'Carrie Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury (uncredited)', 332], 'Kenneth Branagh': ['Asgardian Distress Call (voice) (uncredited)', 357], 'Chris Evans': ['Steve Rogers / Captain America', 2], 'Chris Hemsworth': ['Thor Odinson', 6], 'Tom Vaughan-Lawlor': ['Ebony Maw', 97], 'Robert Pralgo': ['Thanos Reader', 344]}
def f(x):
return x[1][1]
print(dict(sorted(d.items(), key=f)))