getting the key index in a Python OrderedDict?

后端 未结 3 2011
慢半拍i
慢半拍i 2020-12-10 11:52

I have a collections.OrderedDict with a list of key, value pairs. I would like to compute the index i such that the ith key matches a given value.

3条回答
  •  孤街浪徒
    2020-12-10 12:16

    Basically, no. OrderedDict gets its ability to look things up quickly by key name just by using a regular, unordered dict under the hood. The order information is stored separately in a doubly linked list. Because of this, there's no way to go directly from the key to its index. The order in an OrderedDict is mainly intended to be available for iteration; a key does not "know" its own order.

提交回复
热议问题