python – How to convert Counter object to dict?

python – How to convert Counter object to dict?

A Counter is already a dict – or, a subclass of it. But, if you really need exactly a dict for some reason, then its a one-liner:

>>> c = Counter(word1=4, word2=3)
>>> c
Counter({word1: 4, word2: 3})
>>> dict(c)
{word1: 4, word2: 3}

Any Mapping (anything that behaves like a dictionary) can be passed into dict, and you will get a dict with the same contents. There is no need to iterate over it to construct it yourself.

This gives you one loop, with one line in the body instead of a nested loop. But any code of the form:

 thing = a new empty collection
 for elem in old_thing:
    Add something to do with elem to thing

Can usually be done in one line using a generator expression or a list, set or dict comprehension. Were building a dict, so a dict comprehension (the Examples section is what youre most interested in) seems likely. Ill leave coming up with it as an exercise for the reader. 😉

Maybe you are looking for:

>>> from collections import defaultdict
>>> pair = defaultdict(dict)
>>> pair[3][2]=hello
>>>
>>> pair
defaultdict(<type dict>, {3: {2: hello}})
>>>
>>> pair[3]
{2: hello}
>>> 

python – How to convert Counter object to dict?

new_pair = {} # simple dict at the top level
for doc, tab in testing.form.items():
    for word, freq in tab.items():
        # top-level values is word counters
        new_pair[doc].setdefault(word, Counter()) += freq

Leave a Reply

Your email address will not be published. Required fields are marked *