Retreive JSON Keys In Python -


i set following json code dictionary called json_data in python 3. add every value of class in classes list called lst.

json code:

{     "images": [       {         "classifiers": [           {             "classes": [               {                 "class": "street",                 "score": 0.846               },               {                 "class": "road",                 "score": 0.85               },               {                 "class": "yellow color",                 "score": 0.872               },               {                 "class": "green color",                 "score": 0.702               }             ],             "classifier_id": "default",             "name": "default"           }         ],         "image": "images/parisstreets/paris-streets-1.jpg"       }    ],    "images_processed": 1 } 

the following output json structure above.

{'street','road','yellow color','green color'} 

here code tried:

lst = list() def func(json_data):     item in json_data['images'][0]['classifiers']['classes']:         class1 = item['class']         lst.append(class1)     return(lst) 

here error/traceback getting when trying run code:

typeerror: list indices must integers or slices, not str 

so looks trying add string list list take integer or slice. can not find out slice can not convert text string. how fix this?

thanks! brendan (python 3.6.1, windows 10 64bit)

the first issue classifiers list, need additional index @ classes. work:

for item in json_data['images'][0]['classifiers'][0]['classes']: 

second, want result set not list, can do: return set(lst). note sets unordered, don't expect ordering of items match of list.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -