So, this is probably a stupid question, but I have a mapping of unique IDs to unique values. Sometimes I want the value for a certain ID, sometimes I want to know the ID of a certain value. I search more than I modify the collection. I'm wondering if there's a special datastructure that makes sense here, or if I should 开发者_如何学Pythonjust maintain two copies of the collection (which is never super large)--one keyed by ID, and one keyed by value.
Thanks!
Maintaining two copies of the collection is the canonical solution.
Note that the two directions can use different collection types if appropriate (e.g. hash tables with different hash functions, a hash table and a balanced tree, etc.).
Something like Google's BiMap.
精彩评论