python - Compute Perentages of Values in Dictionary -


i'm not experienced in python , have 1 last modification need make data set. data looks this:

"bone": {     "sev": 12,      "er": 16,      "fam": 177 },  "fracture": {     "sev": 76 },  "chest pain": {     "er": 6 },  

it large number of keys, each 1 10 or labels. want change numbers probabilities. so, instance,

"bone": { "sev": .05,  "er": .07,  "fam": .86 },  

so, sum total sum of tags , divide each tag's value sum. i'm not sure how iterate through these in efficient way.

edit: code i'm trying , not working

with open('probability.json') data_file:         data = json.load(data_file)  loadedd = json.loads(data)   def calculate_percentage(labels):      total = float(sum(labels.values()))      return {k: v / total k,v in labels.items()}  section in loadedd:      section = calculate_percentage(section) 

gives error

    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) typeerror: expected string or buffer 

data = {'bone': {'sev': 12,                   'er': 16,                   'fam': 177                  }           }      injury, ward_dict in data.iteritems():      total = float(sum(ward_dict.values()))     percentage = [number / total number in ward_dict.values()]     data_out = {injury: {k: v k, v in zip(ward_dict.keys(), percentage)} }     print(data_out) 

which print out new dictionary percentages keys:

{'bone': {'sev': 0.05853658536585366, 'er': 0.07804878048780488, 'fam': 0.8634146341463415}} 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -