开发者

How to sort dict of dicts by keys?

开发者 https://www.devze.com 2023-04-01 11:30 出处:网络
I have a dict, that looks like: channels = { \'24\': {\'type\': \'plain\', \'table_name\': \'channel.items.AuctionChannel\'},

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': 'pl开发者_开发技巧ain', 'table_name': 'channel.gm.AvatarMoneyChannel'}, 
}

I have read articles, but i don't understand how to sort by keys of main dict.


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:

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


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)


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.


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):
    ....


result=collections.OrderedDict(sorted(your_dict.items()))


try with something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消