mxrdybxm
mxrdybxm

Reputation: 13

How to round off values in a combined dictionary

I have a combined dictionary called frequencies which compares the frequency of the same word in two different sets of texts:

{'banana': [0, 0.0001505742903433696], 'pear': [0, 2.1510612906195657e-05], 'pineapple': [0, 2.1510612906195657e-05], 'orange': [0, 4.3021225812391315e-05], 'apple': [0, 2.1510612906195657e-05]...}

I want to round and reduce decimals so it looks like this, for instance: {'pear': [0, 2,15]}

I have tried this code:

for x in frequencies:
for key, value in frequencies.items():
        frequencies[key]=round(float(value), 2)

But I keep getting this error:

     frequencies[key]=round(float(value), 2)
TypeError: float() argument must be a string or a number, not 'list'

Upvotes: 0

Views: 49

Answers (1)

I'mahdi
I'mahdi

Reputation: 24069

This happen because your values is a list. you need iterate over values then round them:

for key, value in frequencies.items():
    frequencies[key] = [round(v,2) for v in value] 

Upvotes: 2

Related Questions