How to combine the number of lines and the number of lines

I have a set of line counters in the form of set, and I want to split it into some int value.

Example:

 Counter({'dlr': 21, 'said': 18, 'total': 17, 'bankamerica': 13, 'bank': 11, 'analyst': 9, 'prev': 9, 'februari': 8, 'york': 8, 'would': 8, 'price': 8, 'time': 8, 'wheat': 7})

I want to divide it by some int value to get a word with a score divided by this int value.

I get TypeError: I can not concatenate the objects 'str' and 'int'.

+4
source share
2 answers

Build a new counter with dict understanding:

>>> counter = Counter({'dlr': 21, 'said': 18, 'total': 17, 'bankamerica': 13, 'bank': 11, 'analyst': 9, 'prev': 9, 'februari': 8, 'york': 8, 'would': 8, 'price': 8, 'time': 8, 'wheat': 7})
>>> counter2 = Counter({k:v/2 for k,v in counter.items()})
>>> counter2
Counter({'dlr': 10, 'said': 9, 'total': 8, 'bankamerica': 6, 'bank': 5, 'would': 4, 'price': 4, 'februari': 4, 'york': 4, 'time': 4, 'prev': 4, 'analyst': 4, 'wheat': 3})

If you do not need integer division:

>>> from __future__ import division
>>> counter2 = Counter({k:v/2 for k,v in counter.items()})
>>> counter2
Counter({'dlr': 10.5, 'said': 9.0, 'total': 8.5, 'bankamerica': 6.5, 'bank': 5.5, 'prev': 4.5, 'analyst': 4.5, 'would': 4.0, 'price': 4.0, 'februari': 4.0, 'york': 4.0, 'time': 4.0, 'wheat': 3.5})
+4
source

Assuming you meant Counter = {...}:

for key in Counter.keys():
    Counter[key] /= value
0
source

Source: https://habr.com/ru/post/1526221/


All Articles