index to class return in python

2394 views python

I have implemented class to index. That is when I call (_class_to_ind) with class name, index wrt to the class is returned.

_classes = ('__background__', 'plate', 'one', 'two')
_class_to_ind = dict(zip(_classes, xrange(_classes)))

Now is I like to make it reverse as

_classes = ('__background__', 'plate', 'one', 'two')
_ind_to_classes = ???????????

When I call _ind_to_classes(1), I have return class. How can i do that? I prefer python2.7.

answered question

Did you try reversing the arguments of zip?

let me test, thanks

If you're saying "how can I pass in 1 and get the element at index 1 of _classes?", why not just regular indexing: _classes[1]?

you might as well add xrange(len(_classes)) #notice the len or else it would throw TypeError

1 Answer


I tried this

_class_to_ind = dict(zip(xrange(len(_classes)), _classes))

the output was

_class_to_ind[1] >> 'plate'

is this what you are looking for?

posted this

Have an answer?


Please login first before posting an answer.