我有这本词典:
statuses = {
'pending' : {'status_for':'all', 'position':1},
'cancelled' : {'status_for':'all','position':2},
'approved' : {'status_for':'owner', 'position':1},
'rejected - owner' : {'status_for':'owner', 'position':2},
'accepted' : {'status_for':'dev', 'position':1},
'rejected - developer' : {'status_for':'dev', 'position':3},
'closed' : {'status_for':'dev', 'position':5},
}
我还有一个函数可以提取
status_for
或 owner
的所有 dev
值,如下所示,并将其放入 PyQt QComboBox 中:
for s in statuses:
if statuses[s]['status_for'] == "dev" or statuses[s]['status_for'] == "all":
cb_developer_status.addItem(s.capitalize(), s)
我想按
position
值来订购这些。什么是执行此操作的好方法,以便当我通过组合框填充时我可以按预定义的顺序进行操作?
我意识到上面的代码片段正在检查“dev”和“all”,我现在的假设是我必须循环字典两次才能按照我希望的顺序获取两个单独的块(即。' all' 出现在 'dev' 之前)。
我看到了这篇文章,但我不确定如何将此答案转换为 是字典中的字典。
这样的东西有用吗?与您链接的帖子类似,它使用
key
的 sorted
函数来提供自定义排序顺序。 iteritems()
返回一个 (key, value)
元组,以便传递到 lambda (x, y): y['position']
,其中 y['position']
是值(您的嵌套字典,由状态键入),position
是您要排序的项目.
In [35]: statuses = {
'pending' : {'status_for':'all', 'position':1},
'cancelled' : {'status_for':'all','position':2},
'approved' : {'status_for':'owner', 'position':1},
'rejected - owner' : {'status_for':'owner', 'position':2},
'accepted' : {'status_for':'dev', 'position':1},
'rejected - developer' : {'status_for':'dev', 'position':3},
'closed' : {'status_for':'dev', 'position':5},
}
In [44]: for s in sorted(statuses.iteritems(), key=lambda (x, y): y['position']):
....: print s
....:
....:
('accepted', {'position': 1, 'status_for': 'dev'})
('approved', {'position': 1, 'status_for': 'owner'})
('pending', {'position': 1, 'status_for': 'all'})
('rejected - owner', {'position': 2, 'status_for': 'owner'})
('cancelled', {'position': 2, 'status_for': 'all'})
('rejected - developer', {'position': 3, 'status_for': 'dev'})
('closed', {'position': 5, 'status_for': 'dev'})
In [232]: statuses = {
'pending' : {'status_for':'all', 'position':1},
'cancelled' : {'status_for':'all','position':2},
'approved' : {'status_for':'owner', 'position':1},
'rejected - owner' : {'status_for':'owner', 'position':2},
'accepted' : {'status_for':'dev', 'position':1},
'rejected - developer' : {'status_for':'dev', 'position':3},
'closed' : {'status_for':'dev', 'position':5},
}
In [235]: sorted(statuses,key=lambda x:statuses[x]['position'])
Out[235]:
['accepted',
'approved',
'pending',
'rejected - owner',
'cancelled',
'rejected - developer',
'closed']
或使用
operator.getitem()
:
In [260]: from operator import *
In [261]: sorted(statuses.items(),key=lambda x:getitem(x[1],'position'))
Out[261]:
[('accepted', {'position': 1, 'status_for': 'dev'}),
('approved', {'position': 1, 'status_for': 'owner'}),
('pending', {'position': 1, 'status_for': 'all'}),
('rejected - owner', {'position': 2, 'status_for': 'owner'}),
('cancelled', {'position': 2, 'status_for': 'all'}),
('rejected - developer', {'position': 3, 'status_for': 'dev'}),
('closed', {'position': 5, 'status_for': 'dev'})]
我知道现在回答这个问题已经很老了,但我遇到了一个类似的问题,字典结构几乎与你相同,这就是我解决它的方法,
我的结构是这样的:
dictOfDicts = {
0: {'id': 3, 'title': 'hello'},
1: {'id': 1, 'title': 'hi'},
2: {'id': 2, 'title': 'aloha'},
}
我想命令它是这样的:
dictOfDicts = {
0: {'id': 1, 'title': 'hi'},
1: {'id': 2, 'title': 'aloha'}
2: {'id': 3, 'title': 'hello'},
}
即使用内部
'id'
项目更改要订购的外部项目。
所以我首先使用
sorted()
: 在外部项目列表中获取新索引
newIndex = sorted(dictOfDicts, key=lambda x: dictOfDicts[x]['id'])
现在
newIndex
在列表中有新的顺序
#output
newIndex = [1,2,0]
然后我用它来更改外部项目的顺序,使用
newIndex
:
dictOfDicts = {newIndex[k]: dictOfDicts[k] for k in newIndex}
如果有人想知道到底发生了什么,请在评论中告诉我,我不希望答案超过必要的长度,因为问题已经得到解答。
这是对 python dict of dict(字典的字典)进行排序的另一种最佳方法:
# define dictionary data
dictOfDict = {
'mainkey1': {'subkey1':'a', 'subkey2': 100},
'mainkey2': {'subkey1':'b', 'subkey2': 50},
'mainkey3': {'subkey1':'c', 'subkey2': 500},
'mainkey4': {'subkey1':'d', 'subkey2': 250}
}
# define another dictionary to store sorted dictionary data
sorted_dictOfDict = dict()
# sort mainkeys by subkey2 :: outputs a list of mainkeys from original dict
sorted_keys = sorted(dictOfDict, key=lambda x:dictOfDict[x]['subkey2'])
# use the sorted list of keys to copy original dict to sorted dict (key-val by key-val)
for key in sorted_keys:
sorted_dictOfDict[key] = dictOfDict[key]
# clear original dict and copy sorted dict back to original
dictOfDict.clear()
dictOfDict = sorted_dictOfDict.copy()
print(dictOfDict)
输出:
dictOfDict = {
'mainkey2': {'subkey1':'b', 'subkey2': 50},
'mainkey1': {'subkey1':'a', 'subkey2': 100},
'mainkey4': {'subkey1':'d', 'subkey2': 250},
'mainkey3': {'subkey1':'c', 'subkey2': 500}
}