Find dictionary items whose key matches a substring

前端 未结 5 468
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 20:28

I have a large dictionary constructed like so:

programs[\'New York\'] = \'some values...\' 
programs[\'Port Authority of New York\'] = \'some values...\' 
pr         


        
5条回答
  •  情深已故
    2020-12-07 20:56

    You should use the brute force method given by mensi until it proves to be too slow.

    Here's something that duplicates the data to give a speedier lookup. It only works if your search is for whole words only - i.e. you'll never need to match on "New Yorks Best Bagels" because "york" and "yorks" are different words.

    words = {}
    for key in programs.keys():
        for w in key.split():
            w = w.lower()
            if w not in words:
                words[w] = set()
            words[w].add(key)
    
    
    def lookup(search_string, words, programs):
        result_keys = None
        for w in search_string.split():
            w = w.lower()
            if w not in words:
                return []
            result_keys = words[w] if result_keys is None else result_keys.intersection(words[w])
        return [programs[k] for k in result_keys]
    

    If the words have to be in sequence (i.e. "York New" shouldn't match) you can apply the brute-force method to the short list of result_keys.

提交回复
热议问题