fastest way to search python dict with partial keyword

前端 未结 2 1310
后悔当初
后悔当初 2020-12-10 11:49

What is the fastest way to determine if a dict contains a key starting with a particular string? Can we do better than linear? How can we achieve an O(1) operation when we o

2条回答
  •  情歌与酒
    2020-12-10 12:20

    You could put all the prefixes of the inserted keys to the dict, so for key foo you would insert f, fo and foo. You would have O(1) lookup, but you would spend time on preprocessing (O(k), where k is a key length), and waste lots of memory:

    def insert_with_prefixes(key, value, dict_):
      prefixes = (key[:i+1] for i in xrange(len(key)))
      dict_.update((prefix, value) for prefix in prefixes)
    

    For everyday use I would go (and I go) with the method in arshajii's answer. And of course, have in mind possible many collisions for short prefixes (here: "h"):

    >>> a = {}
    >>> insert_with_prefixes('hello', 'world', a)
    >>> insert_with_prefixes('homo', 'sapiens', a)
    >>> a
    {'h': 'sapiens', 'hom': 'sapiens', 'homo': 'sapiens', 'ho': 'sapiens', 
     'hel': 'world', 'hell': 'world', 'hello': 'world', 'he': 'world'}
    

提交回复
热议问题