0

I have a small script that create JSON. I want to add field track_ID and this field wll be int. Idea is to add some loop in which it starts from 1 and finish when objects gone. Any ideas?

 for list in obj['frames']['objects']:
                        data = {
                            'track_ID': 
                            'object_id': obj['info']['doc']
                        }
Alexx
  • 39
  • 3

3 Answers3

1

Just add an i in it, something like this

i = 0
for list in obj['frames']['objects']:
                        data = {
                            'track_ID': i
                            'object_id': obj['info']['doc']
                        }
                        i = i + 1
Oliver Hnat
  • 797
  • 4
  • 19
1

enumerate() will do that beautifully

MaxTechniche
  • 141
  • 5
0

You can use enumerate. Note that your loop overrides the value of data in every iteration, list is a saved word in python and you don't actually use the value of list in your code example.

for idx, list in enumerate(obj['frames']['objects']):
     data = {
        'track_ID': idx+1 
         'object_id': obj['info']['doc']
     }
Tom Ron
  • 5,906
  • 3
  • 22
  • 38