自定义排序 Python 字典 [英] Custom Sorting Python Dictionary

查看:81
本文介绍了自定义排序 Python 字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一本字典,打印时看起来像这样:

{'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}

我想以我定义的自定义方式对这些进行排序.假设我希望它的排序方式(按键)是 ZT21, 10, WX21, UM, 5, 2.

有人知道如何以预定义/自定义的方式整理字典吗?我正在做的是从数据库中获取这本字典,它可以有 20 多个键,所有键都有特定的顺序.顺序总是被设置,但有时某些键/值不会在字典中.所以这也可能发生:

{'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}

排序(按key)是ZT21, 10, WX21, UM, 5, 2.

所以在这个例子中 10 不存在,但我需要的排序仍然相同,10 将不存在.

有什么想法吗?

解决方案

Python 中的字典是无序的.你可以得到你需要的结果作为 list

<预><代码>>>>d = {'10':-10,'ZT21':14,'WX21':12,'2':15,'5':-3,'UM':-25}>>>keyorder = ['ZT21', '10', 'WX21', 'UM', '5', '2']>>>sorted(d.items(), key=lambda i:keyorder.index(i[0]))[('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)]

或作为 OrderedDict

<预><代码>>>>从集合导入 OrderedDict>>>OrderedDict(sorted(d.items(), key=lambda i:keyorder.index(i[0])))OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])

如果你做了很多这样的事情,使用 dict 作为 keyorder 会更有效率

<预><代码>>>>keyorder = {k:v for v,k in enumerate(['ZT21', '10', 'WX21', 'UM', '5', '2'])}>>>OrderedDict(sorted(d.items(), key=lambda i:keyorder.get(i[0])))OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])

So I have a dictionary that looks like this when I print it:

{'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}

I want to sort these in a custom manner, which I define. Let's say the way I want it to be sorted (by key) is ZT21, 10, WX21, UM, 5, 2.

Anyone know how to go about sorting out a dictionary in a predefined/custom manner? What I am doing is getting this dictionary from a database, and it can come out with over 20 keys, all of which have a specific order. The order is always set, but sometimes certain keys/values wouldn't be in the dictionary. So this could happen too:

{'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}

sorted (by key) is ZT21, 10, WX21, UM, 5, 2.

So the 10 isn't there in this example, but the sorting I need is still the same, the 10 would just be absent.

Any ideas?

解决方案

Dictionaries in Python are unordered. You can get the results you need as a list

>>> d = {'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}
>>> keyorder = ['ZT21', '10', 'WX21', 'UM', '5', '2']
>>> sorted(d.items(), key=lambda i:keyorder.index(i[0]))
[('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)]

or as an OrderedDict

>>> from collections import OrderedDict
>>> OrderedDict(sorted(d.items(), key=lambda i:keyorder.index(i[0])))
OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])

If you are doing a lot of these, it will be more efficient to use a dict for the keyorder

>>> keyorder = {k:v for v,k in enumerate(['ZT21', '10', 'WX21', 'UM', '5', '2'])}
>>> OrderedDict(sorted(d.items(), key=lambda i:keyorder.get(i[0])))
OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])

这篇关于自定义排序 Python 字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆