Removing 'Counter' from Dictionary python

懵懂的女人 提交于 2019-12-11 03:22:29

问题


I am using the Counter class in Python to count the number of words in a key of a large python dictionary. I am creating a new dictionary with the keys being incremented numbers and the values being the return of the counter function. This is what my new dictionary looks like:

TP = {1:Counter({u'x':1, u'b':1, u'H':3}),2:Counter({u'j':1, u'm':4, u'e':2})...}

What I want to do, if possible, is remove the Counter and the parentheses ( and ) so that my dictionary looks like:

TP = {1:{u'x':1, u'b':1, u'H':3}, 2:{u'j':1, u'm':4, u'e':2}...}

If that's not possible, can someone please tell me how to access the nested dictionaries inside the ()? Thank you


回答1:


I don't see any point of doing this, Counter is a subclass of dict and supports all of it's methods so you should have no problems leaving it as is, although just for the sake of this question I will convert it back to a normal dict:

>>> from collections import Counter
>>> tp = {1:Counter({u'x':1, u'b':1, u'H':3}),2:Counter({u'j':1, u'm':4, u'e':2})}
>>> dict((k, dict(v)) for k, v in tp.iteritems())
{1: {u'x': 1, u'b': 1, u'H': 3}, 2: {u'e': 2, u'j': 1, u'm': 4}}

For Python 2.7+ (as suggested by @Blender)

{k: dict(v) for k, v in tp.iteritems()}

Iterating through subdictionaries eg:

for w, c in tp[1].iteritems():
    print w, c



回答2:


You might be looking for a pretty print method.

This is a method:

def pprint_counter(c):
    for k, v in c.items():
        l=[u'{}:{}'.format(ks,vs) for ks,vs in v.items()]
        print '{:4}: {}'.format(k, ' '.join(l))

Now test with your example:

tp = {1:Counter({u'x':1, u'b':1, u'H':3}),2:Counter({u'j':1, u'm':4, u'e':2})}
pprint_counter(tp)

Prints:

   1: x:1 b:1 H:3
   2: m:4 j:1 e:2

Now access the sub element 'x':

tp[1]['x']=123
pprint_counter(tp)

Prints:

   1: x:123 b:1 H:3
   2: m:4 j:1 e:2

So the individual elements you access by subscripting directly `tp[1]['x'] and the formating you desire -- just add code.



来源:https://stackoverflow.com/questions/11731633/removing-counter-from-dictionary-python

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