python - Dynamic Dictionary Field Names -


i'm trying create dictionary dynamic names ( m['field1'], m['field2'], etc). i'm getting error:

typeerror: string indices must integers, not str

index = 0 in results:         metrics['users']['total']['month' + str(index)] = results[index][1]         index = index + 1 

when dictionary doesn't have specific key (e.g. when metrics empty , doesn't have users key), reading dictionary key (i.e. metrics['users']) error. i'm not sure want, following code runs fine:

metrics = {'users': {'total': {}}} results = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] index = 0 in results:     metrics['users']['total']['month' + str(index)] = results[index][1]     index = index + 1 

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 -