I need a data structure that supports FAST insertion and deletion of (key, value) pairs, as well as "get random key", which does the same thing as random.choice(dict.keys()) for a dictionary. I've searched on the internet, and most people seem to be satisfied with the random.choice(dict.keys()) approach, despite it being linear time.
I'm aware that implementing this faster is possible:
I could use a resizing hash table. If I maintain that the ratio of keys to slots is between 1 and 2, then I can just choose random indices until I hit a non-empty slot. I only look at 1 to 2 keys, in expectation.
I can get these operations in guaranteed worst case O(log n) using an AVL tree, augmenting with rank.
Is there any easy way to get this in Python, though? It seems like there should be!
解决方案
This may not specifically relevant to the specific use case listed above, but this is the question I get when searching for a way to nicely get a hold of "any" key in a dictionary.
If you don't need a truly random choice, but just need some arbitrary key, here are two simple options I've found:
key = next(iter(d)) # may be a little expensive, but presumably O(1)
The second is really useful only if you're happy to consume the key+value from the dictionary, and due to the mutation(s) will not be as algorithmically efficient:
key, value = d.popitem() # may not be O(1) especially if next step
if MUST_LEAVE_VALUE:
d[key] = value