dictionary - Python `dict` indexed by tuple: Getting a slice of the pie -
let's have
my_dict = { ("airport", "london"): "heathrow", ("airport", "tokyo"): "narita", ("hipsters", "london"): "soho" }
what efficient (no scanning of keys), yet elegant way airports out of dictionary, i.e. expected output ["heathrow", "narita"]
. in databases can index tuples, it's possible
airports = my_dict.get(("airport",*))
(but 'stars' sitting @ rightmost places in tuple since index stored in 1 order).
since imagine python index dictionary tuple keys in similar way (using keys's inherent order), imagine there might method use slice index way?
edit1: added expected output
edit2: removed last phrase. added '(no scanning of keys)' conditions make clearer.
the way data organized doesn't allow efficient lookup - have scan all keys.
dictionaries hash tables behind scenes, , way access value hash of key - , that, need whole key.
use nested hierarchy this, can direct o(1) lookup:
my_dict = { "airport": { "london": "heathrow", "tokyo": "narita", }, "hipsters": { "london": "soho" } }
Comments
Post a Comment