如何按键对字典中的字典进行排序?
我有一个字典,看起来像:
channels = {
'24': {'type': 'plain', 'table_name': 'channel.items.AuctionChannel'},
'26': {'type': 'plain', 'table_name': 'channel.gm.DeleteAvatarChannel'},
'27': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyChannel'},
'20': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyAssertChannel'},
'21': {'type': 'plain', 'table_name': 'channel.gm.AvatarKillMobComplexChannel'},
'22': {'type': 'plain', 'table_name': 'channel.gm.DistributionMarkChannel'},
'23': {'type': 'plain', 'table_name': 'channel.gm.MailChannel'}
}
我想按键对其进行排序('24','26','27'等...),它应该是这样的:
channels = {
'20': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyAssertChannel'},
'21': {'type': 'merged', 'table_name': 'channel.gm.AvatarKillMobComplexChannel'},
'22': {'type': 'plain', 'table_name': 'channel.gm.DistributionMarkChannel'},
'23': {'type': 'plain', 'table_name': 'channel.gm.MailChannel'}
'24': {'type': 'merged', 'table_name': 'channel.items.AuctionChannel'},
'26': {'type': 'plain', 'table_name': 'channel.gm.DeleteAvatarChannel'},
'27': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyChannel'},
}
我读过文章,但我没有了解如何按主字典的键排序。
I have a dict, that looks like:
channels = {
'24': {'type': 'plain', 'table_name': 'channel.items.AuctionChannel'},
'26': {'type': 'plain', 'table_name': 'channel.gm.DeleteAvatarChannel'},
'27': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyChannel'},
'20': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyAssertChannel'},
'21': {'type': 'plain', 'table_name': 'channel.gm.AvatarKillMobComplexChannel'},
'22': {'type': 'plain', 'table_name': 'channel.gm.DistributionMarkChannel'},
'23': {'type': 'plain', 'table_name': 'channel.gm.MailChannel'}
}
i want to sort it by keys('24','26','27', etc...), it should be like:
channels = {
'20': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyAssertChannel'},
'21': {'type': 'merged', 'table_name': 'channel.gm.AvatarKillMobComplexChannel'},
'22': {'type': 'plain', 'table_name': 'channel.gm.DistributionMarkChannel'},
'23': {'type': 'plain', 'table_name': 'channel.gm.MailChannel'}
'24': {'type': 'merged', 'table_name': 'channel.items.AuctionChannel'},
'26': {'type': 'plain', 'table_name': 'channel.gm.DeleteAvatarChannel'},
'27': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyChannel'},
}
I have read articles, but i don't understand how to sort by keys of main dict.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
字典未排序/排序。如果您使用的是 python 2.7,则可以使用 OrderedDict。
Dicts are not sorted/ordered. If you are using python 2.7 you can use an OrderedDict though.
标准字典未排序。但是,您可以按排序顺序迭代其键和值:
Standard dict is not sorted. You can however iterate over its keys and values in a sorted order:
dict
是一个映射,键没有顺序。这是由于dict()
类型的实现方式造成的:键被散列(使用hash()
内置函数),并且您观察到的顺序源自该散列。您将需要字典的有序字典来保持其顺序并允许您对键进行排序。
collections.OrderedDict
类型是内置的有序字典对于 python 3.x。这是对数据进行排序的示例:
a
dict
is a mapping, keys are not ordered. this is due to the way thedict()
type is implemented: keys are hashed (using thehash()
builtin function) and the order you observe derives from this hash.you will need an ordered dict for the dictionary to keep its ordering and to allow you to sort the keys. the
collections.OrderedDict
type is the builtin ordered dict for python 3.x.here is an example of sorting your data:
字典未排序。但如果你想以所描述的方式进行迭代,你可以这样做:
结果:
或使用collections.OrderedDict。
A dict is unsorted. But if you want to iterate in the described manner, you can do it as follows:
Results in:
Or use collections.OrderedDict.
字典的键没有顺序,顺序无法保证。如果您想创建字典列表,您将丢失关键信息。
如果您需要排序迭代,您可以使用
A dict doesn't have its keys in a order, the order is not guaranteed. If you want to create a list of dictionaries you'll lose the key info.
If you need sorted iterations you can use
尝试使用类似的东西:
try with something like: