【发布时间】:2014-11-10 21:57:11
【问题描述】:
所以我现在已经被困了整整一个小时。我看过其他关于同一问题的帖子,但我无法让我的工作。
这是我要排序的字典中的字典:
diction = {'z': {'golf': 3, 'bowling': 9}, 'a': {'fed': 5, 'alvin': 10}, 'r': {'yell': 7, 'shout': 11}}
我试图首先对字典的最外层进行排序,这就是 t[0] 出现的地方。然后我想按字母顺序对与字母配对的元素进行排序。想要的输出——
{a:{alvin:10, fed:5}, r:{shout:11, yell:7}, z:{bowling:9, golf:3}}
这是我的代码:
import collections
diction = {'z': {'golf': 3, 'bowling': 9}, 'a': {'fed': 5, 'alvin': 10}, 'r': {'yell': 7, 'shout': 11}}
a= collections.OrderedDict(sorted(diction.items(),key=lambda t:t[0][1]))
这显然行不通。
编辑:
所以到目前为止,这只是按字母排序。我得到:
{a: {fed:5, alvin:10}, r:{yell:7, shout:11}, z:{golf:3, bowling:9}}
我希望它显示什么:
{a:{alvin:10, fed:5}, r:{shout:11, yell:7}, z:{bowling:9, golf:3}}
【问题讨论】:
-
你能描述一下“明显”的问题吗?
标签: python sorting dictionary lambda ordereddictionary