Python multiplying every dictionary value by single number -


i know question has been asked before having difficult time following examples previous answers code work. have dictionary 12 keys, each having size of 102. want multiply every value in each key single number. following these examples (python: perform operation on each dictionary value) here have tried:

my_data.update((x , y*1e6)for x, y in my_data.items()) 

this returns error "can't multiply sequence non-int of type 'float'. if change number float number (1e6 2) doubles size of each key.

i tried one:

for key in my_data:     my_data[key] *= 2 

all double size of each key (102 204). can multiply individual key desired number so:

my_data['ap_33.txt_dose'] = [x * 1e6 x in my_data['ap_33.txt_dose']] 

edit:

it requested show my_data looks like. i'm not sure how best display importing data 12 different text files dictionary. each name though (key_1, key_2, etc.) , each key has 102 float values (0, 2.88e-9, 9.6e-9, etc.)

your values either lists or strings (some form of iterable). can multiply sequence integer, not float. change update to:

my_data.update((x , y*int(1e6))for x, y in my_data.items()) 

which coerce away decimal produced 1e6, it'd more appropriate clean data not contain sequences (unless attempting produce 1000000 item sequences @ each key in dict).


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -