如何按键对字典中的字典进行排序?

发布于 2024-12-02 04:48:05 字数 1227 浏览 1 评论 0原文

我有一个字典,看起来像:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

﹂绝世的画 2024-12-09 04:48:05

字典未排序/排序。如果您使用的是 python 2.7,则可以使用 OrderedDict。

Dicts are not sorted/ordered. If you are using python 2.7 you can use an OrderedDict though.

著墨染雨君画夕 2024-12-09 04:48:05

标准字典未排序。但是,您可以按排序顺序迭代其键和值:

for channelName, channelValue in sorted(channels.items()):
    ...

Standard dict is not sorted. You can however iterate over its keys and values in a sorted order:

for channelName, channelValue in sorted(channels.items()):
    ...
你爱我像她 2024-12-09 04:48:05

dict 是一个映射,键没有顺序。这是由于 dict() 类型的实现方式造成的:键被散列(使用 hash() 内置函数),并且您观察到的顺序源自该散列。

您将需要字典的有序字典来保持其顺序并允许您对键进行排序。 collections.OrderedDict 类型是内置的有序字典对于 python 3.x。

这是对数据进行排序的示例:

import collections

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'}
}

channels = collection.OrderedDict(sorted(channels.items(), key=lambda item: item[0]))
for key,value in channels.items():
    print(key, ':', value)

a dict is a mapping, keys are not ordered. this is due to the way the dict() type is implemented: keys are hashed (using the hash() 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:

import collections

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'}
}

channels = collection.OrderedDict(sorted(channels.items(), key=lambda item: item[0]))
for key,value in channels.items():
    print(key, ':', value)
盛装女皇 2024-12-09 04:48:05

字典未排序。但如果你想以所描述的方式进行迭代,你可以这样做:

for key in sorted(channels):
    print key

结果:

20
21
22
23
24
26
27

或使用collections.OrderedDict。

A dict is unsorted. But if you want to iterate in the described manner, you can do it as follows:

for key in sorted(channels):
    print key

Results in:

20
21
22
23
24
26
27

Or use collections.OrderedDict.

静水深流 2024-12-09 04:48:05

字典的键没有顺序,顺序无法保证。如果您想创建字典列表,您将丢失关键信息。

如果您需要排序迭代,您可以使用

for key in sorted(dict):
    ....

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

for key in sorted(dict):
    ....
二智少女猫性小仙女 2024-12-09 04:48:05
result=collections.OrderedDict(sorted(your_dict.items()))
result=collections.OrderedDict(sorted(your_dict.items()))
秋心╮凉 2024-12-09 04:48:05

尝试使用类似的东西:

print [ {i: channels[i] } for  i in sorted(channels)]

try with something like:

print [ {i: channels[i] } for  i in sorted(channels)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文