0

I have a dict which I have used Counter to get term frequencies for each key e.g. 'A' and 'B'.

dict = {'A': Counter({'food': 30, 'menu': 19, 'good': 15}), 'B': Counter({'one': 5, 'chicken': 10})}

I would like to be able to add a new field so that each term has another value.

I have tried:

for key, values in dict.items():
    for it1, it2 in values:
         dict[key][it1][it2] = 0

but I receive:

ValueError: too many values to unpack (expected 2)

Do I need to remove the Counter object? How do I create a new value and how do I access this value?

2
  • 1
    for it1, it2 in values.items(): Mar 22, 2017 at 21:53
  • The value of values is one counter. You cannot split it into it1 and it2.
    – DYZ
    Mar 22, 2017 at 21:53

3 Answers 3

1

You're better off creating an entirely new dictionary, to have that extra nested dict:

dct = {key: {k: {v: 0} for k, v in values.items()} for key, values in dct.items()}
3
  • but then you lose the Counter capability. Mar 22, 2017 at 21:56
  • forget that. I realized that I don't understand the question. Time to go to bed :) Mar 22, 2017 at 21:59
  • This is exactly what I wanted - thank you. Sorry to other answerers for not phrasing my question more clearly. Mar 22, 2017 at 22:30
1

I hope I understood your question. You want to add one more item to the counter?. If that is the case, you could do by updating the counter object. the count will increase as you keep on updating it.

mydict = {'A': Counter({'food': 30, 'menu': 19, 'good': 15}), 'B': Counter({'one': 5, 'chicken': 10})}
mydict['A'].update(['price'])
mydict['B'].update(['cow'])
print mydict

{'A': Counter({'food': 30, 'menu': 19, 'good': 15, 'price': 1}), 'B': Counter({'chicken': 10, 'one': 5, 'cow': 1})}

if you do update again

mydict['A'].update(['price'])
mydict['B'].update(['cow'])
print mydict

will result in

{'A': Counter({'food': 30, 'menu': 19, 'good': 15, 'price': 2}), 'B': Counter({'chicken': 10, 'one': 5, 'cow': 2})}
0

Without creating a new dict you can manipulate your current dict with Counter inside.

You can print your current items like this way:

dct = {'A': Counter({'food': 30, 'menu': 19, 'good': 15}), 'B': Counter({'one': 5, 'chicken': 10})}

for k, v in dct.items():
    print(k, v)

Output:

>>> A Counter({'food': 30, 'menu': 19, 'good': 15})
>>> B Counter({'chicken': 10, 'one': 5})

In order to modify values inside Counter dict, you can do something like this:

for k, v in dct:
    if "food" in v:
        v["food"] = 666

Output:

print(dct)

>>> {'A': Counter({'food': 666, 'menu': 19, 'good': 15}), 'B': Counter({'chicken': 10, 'one': 5})}

And if you want to add new elements to your Counter dict you can do something like this:

for k, v in dct.items():
    if "food" in v:
        v.update({"new_food":0})

print(dct)

Output:

>>> {'A': Counter({'food': 30, 'menu': 19, 'good': 15, 'new_food': 0}), 'B': Counter({'chicken': 10, 'one': 5})}

Otherwise if you want to have a new "regular" dict you can use dict comprehension:

new_dct = {k:{key:val for key,val in v.items()} for k,v in dct.items()}

print(new_dct)

>>> {'A': {'food': 30, 'menu': 19, 'good': 15}, 'B': {'chicken': 10, 'one': 5}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.