TypeError: must be real number, not dict in python
1
0
Entering edit mode
3.4 years ago
Georgia • 0

Hi, I am trying to use the log10() function in python on a set of numbers in a script. I am having this type of error "TypeError: must be real number, not dict" The list is done by numbers and string.

string python dict • 9.1k views
ADD COMMENT
1
Entering edit mode
3.4 years ago
sbstevenlee ▴ 480

Is this what you are currently doing?

>>> import math
>>> d = {'a': 1, 'b': 10, 'c': 100}
>>> math.log10(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be real number, not dict

If you want to apply the log10 function to each element in the dictionary, try this instead:

>>> [math.log10(value) for key, value in d.items()]
[0.0, 1.0, 2.0]
ADD COMMENT
0
Entering edit mode

@OP If you want to retrieve this info back still in dictionary form (but without updating/overwriting the existing dictionary), you can do it like so based on the code above:

new_d = {key: math.log10(value) for key, value in d.items()}
>>> {'a': 0.0, 'b': 1.0, 'c': 2.0}
ADD REPLY

Login before adding your answer.

Traffic: 1708 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6