keyedcollection - Keyed Collection in Python? -


is there equivalent keyedcollection in python, i.e. set elements have (or dynamically generate) own keys?

i.e. goal here avoid storing key in 2 places, , therefore dictionaries less ideal (hence question).

@mehrdad said:

because semantically, doesn't make sense. when object knows key, doesn't make sense put in dictionary -- it's not key-value pair. it's more of semantic issue else.

with constraint, there nothing in python want. suggest use dict , not worry level of detail on semantics. @gabi purcaru's answer shows how can create object interface want. why bothered how it's working internally?

it c#'s keyedcollection doing same thing under covers: asking object key , storing key fast access. in fact, docs:

by default, keyedcollection(of tkey, titem) includes lookup dictionary can obtain dictionary property. when item added keyedcollection(of tkey, titem), item's key extracted once , saved in lookup dictionary faster searches. behavior overridden specifying dictionary creation threshold when create keyedcollection(of tkey, titem). lookup dictionary created first time number of elements exceeds threshold. if specify –1 threshold, lookup dictionary never created.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -