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

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -