convert a dict to sorted dict in python

[亡魂溺海] 提交于 2019-11-30 03:38:40

问题


I want to convert a dict into sorted dict in python

data = pandas.read_csv('D:\myfile.csv')
for colname, dtype in data.dtypes.to_dict().iteritems():
    if dtype == 'object':
        print colname
        count = data[colname].value_counts()
        d = dict((str(k), int(v)) for k, v in count.iteritems())
        f = dict(sorted(d.iteritems(), key=lambda item: item[1], reverse = True)[:5])
        print f

        m ={}
        m["count"]= int(sum(count))    
        m["Top 5"]= f    
        print m    
        k = json.dumps(m)
        print k    
f = {'Gears of war 3': 6, 'Batman': 5, 'gears of war 3': 4, 'Rocksmith': 5, 'Madden': 3}

My desired Output is :

f = {'Gears of war 3': 6, 'Batman': 5, 'Rocksmith': 5, 'gears of war 3': 4, 'Madden': 3}
k = {'count':24, 'top 5':{'Gears of war 3': 6, 'Batman': 5, 'Rocksmith': 5, 'gears of war 3': 4, 'Madden': 3}}

(in the descending order of values and the result should be a dict)


回答1:


You cannot sort a dict because dictionary has no ordering.

Instead, use collections.OrderedDict:

>>> from collections import OrderedDict
>>> d = {'Gears of war 3': 6, 'Batman': 5, 'gears of war 3': 4, 'Rocksmith': 5, 'Madden': 3}

>>> od = OrderedDict(sorted(d.items(), key=lambda x:x[1], reverse=True))
>>> od
OrderedDict([('Gears of war 3', 6), ('Batman', 5), ('gears of war 3', 4), ('Rocksmith', 5), ('Madden', 3)])

>>> od.keys()
['Gears of war 3', 'Batman', 'gears of war 3', 'Rocksmith', 'Madden']
>>> od.values()
[6, 5, 4, 5, 3]
>>> od['Batman']
5

The "order" you see in an JSON object is not meaningful, as JSON object is unordered[RFC4267].

If you want meaningful ordering in your JSON, you need to use a list (that's sorted the way you wanted). Something like this is what you'd want:

{
  "count": 24,
  "top 5": [
    {"Gears of war 3": 6},
    {"Batman": 5},
    {"Rocksmith": 5},
    {"gears of war 3": 4},
    {"Madden": 3}
  ]
}

Given the same dict d, you can generate a sorted list (which is what you want) by:

>>> l = sorted(d.items(), key=lambda x:x[1], reverse=True)
>>> l
[('Gears of war 3', 6), ('Batman', 5), ('Rocksmith', 5), ('gears of war 3', 4), ('Madden', 3)]

Now you just pass l to m['top5'] and dump it:

m["Top 5"]= l
k = json.dumps(m)


来源:https://stackoverflow.com/questions/13062300/convert-a-dict-to-sorted-dict-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!