1

我有一本看起来像这样的字典:

myDict = {
    'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
    'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
    'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
    'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'},
    'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}
}

我的目标是按每个子词典的 thesite_location和 the对这本词典进行排序。serial_num

使用我在这个问题中找到的代码 -对字典字典进行排序 python - 我能够对其进行排序,但这并不是我所期望的。

这是我的代码:

import pprint
items = ((k, k2, v) for k in myDict for k2, v in myDict[k].items())
ordered = sorted(items, key=lambda x:x[-1], reverse=False)
pprint.pprint(ordered)

这是我得到的结果:

[('ABC12346', 'site_location', 'Europe'),
 ('SER12345', 'site_location', 'North America'),
 ('SER12346', 'site_location', 'North America'),
 ('SER12347', 'site_location', 'South America'),
 ('ABC12345', 'site_location', 'South America'),
 ('ABC12346', 'serial_num': 'ABC12346'),
 ('SER12345', 'serial_num': 'SER12345'),
 ('SER12346', 'serial_num': 'SER12346'),
 ('SER12347', 'serial_num': 'SER12347'),
 ('ABC12345', 'serial_num': 'ABC12345')]

我期待着更像这样的东西:

{
    'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}
    'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
    'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
    'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'},
    'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
}

实际结果是将序列号和站点位置分开。我想把它们放在排序的对象中。我怎样才能做到这一点?

4

4 回答 4

4

这是你想要的吗?

dicts = [{k: v} for (k,v) in myDict.items()]
dicts.sort(key=lambda d: (d.values()[0]['site_location'], d.values()[0]['serial_num'],))

输出:

import pprint
pprint.pprint(dicts)

是:

[{'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}},
 {'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'}},
 {'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'}},
 {'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'}},
 {'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'}}]

编辑:我正在回答你的输出格式,但这可能更有意义:

dicts = myDict.items()
dicts.sort(key=lambda (k,d): (d['site_location'], d['serial_num'],))

输出:

[('ABC12346', {'serial_num': 'ABC12346', 'site_location': 'Europe'}),
 ('SER12345', {'serial_num': 'SER12345', 'site_location': 'North America'}),
 ('SER12346', {'serial_num': 'SER12346', 'site_location': 'North America'}),
 ('ABC12345', {'serial_num': 'ABC12345', 'site_location': 'South America'}),
 ('SER12347', {'serial_num': 'SER12347', 'site_location': 'South America'})]
于 2012-07-05T14:18:58.600 回答
2

字典不保留项目的顺序 - 因此无法排序。如果您想要排序字典的外观,您需要制作一个排序列表,然后将其插入 OrderedDict 类。下面的代码片段说明了这一点:

from collections import OrderedDict

myDict = {
    'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
    'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
    'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
    'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'},
    'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}
}

def sortfun(d):
    return (d[1]['site_location'], d[1]['serial_num'])

skv = sorted(myDict.iteritems(), key=sortfun)
sorted_dict = OrderedDict(skv)

print sorted_dict
于 2012-07-05T14:28:14.547 回答
1

我有一个解决方案cmp(我认为你必须使用它,因为我们混合使用 2 个键)但它不是很漂亮,我想它可以改进:

>>> pprint(sorted(myDict.items(), cmp=lambda x, y: cmp((x[1]['site_location'], x[1]['serial_num']), (y[1]['site_location'], y[1]['serial_num']))))
[('ABC12346', {'serial_num': 'ABC12346', 'site_location': 'Europe'}),
 ('SER12345', {'serial_num': 'SER12345', 'site_location': 'North America'}),
 ('SER12346', {'serial_num': 'SER12346', 'site_location': 'North America'}),
 ('ABC12345', {'serial_num': 'ABC12345', 'site_location': 'South America'}),
 ('SER12347', {'serial_num': 'SER12347', 'site_location': 'South America'})]
于 2012-07-05T14:23:42.250 回答
1
>>> import pprint
>>> dic=myDict.items()
>>> dic.sort(key=lambda x:(x[1]['site_location'],x[1]['serial_num']))
>>> pprint.pprint([{k:v} for k,v in dic])
[{'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}},
 {'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'}},
 {'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'}},
 {'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'}},
 {'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'}}]
于 2012-07-05T14:32:35.483 回答