当前位置:   article > 正文

python批量搜索关键字_多关键字是高效的搜索关键字

批量 多个关键词 文本搜索

这属于“天真”阵营,但这里有一种方法,将集合作为思考的食粮:docs = [

""" Here's a sentence with dog and apple in it """,

""" Here's a sentence with dog and poodle in it """,

""" Here's a sentence with poodle and apple in it """,

""" Here's a dog with and apple and a poodle in it """,

""" Here's an apple with a dog to show that order is irrelevant """

]

query = ['dog', 'apple']

def get_similar(query, docs):

res = []

query_set = set(query)

for i in docs:

# if all n elements of query are in i, return i

if query_set & set(i.split(" ")) == query_set:

res.append(i)

return res

这将返回:

^{pr2}$

当然,时间复杂度并不是很高,但由于执行哈希/集操作的速度,它比使用列表要快得多。

第2部分是,Elasticsearch是一个很好的候选者,如果您愿意付出努力,并且您要处理大量的数据。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号