简体   繁体   English

给定字符串和(列表)单词,返回包含字符串的单词(最优算法)

[英]Given string and (list) of words, return words that contain string (optimal algorithm)

Let's say we have a list of unique words and a substring.假设我们有一个唯一词列表和一个 substring。

I am looking for an optimal algorithm, that returns words containing the substring.我正在寻找一种最佳算法,它返回包含 substring 的单词。

The general application is: Given a database use search bar to filter the results.一般应用是:给定一个数据库,使用搜索栏过滤结果。

A simple implementation in Python: Python中的一个简单实现:

def search_bar(words, substring):
    ret = []
    for word in words:
        if substring in word:
            ret.append(word)
    return ret

words = ["abc", "bcd", "thon", "Python"]
substring = "on"

search_bar(words, substring)

this would return:这将返回:

["thon", "Python"]

in time O(lenght_of_list * complexity_of_in) , where complexity_of_in depends in some way on the length of the substring and the length of individual words.在时间O(lenght_of_list * complexity_of_in)中, complexity_of_in在某种程度上取决于 substring 的长度和单个单词的长度。

What I am asking is whether there is faster implementation.我要问的是是否有更快的实施。 Given that we can preprocess the list into any structure we want.鉴于我们可以将列表预处理为我们想要的任何结构。

Just redirection to the problem/answer would be amazing.只是重定向到问题/答案将是惊人的。

Note: It would be better if such structure doesn't take too long to add a new word.注意:如果这种结构不需要太长时间来添加一个新单词会更好。 But primarily it doesn't have to be able to add anything, as the Python example doesn't.但主要它不必能够添加任何东西,因为 Python 示例没有。

Also, I am not sure about the tags with this question...另外,我不确定这个问题的标签......

maybe use也许使用

word.find(substring) 

instead反而

substring in word

and as variant:并作为变体:

def search_bar(words, substring):
    return list(filter(lambda word: word.find(substring)!=-1, words))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM