首页 > 解决方案 > Python-搜索两个关键字列表

问题描述

我怎样才能在两个列表上应用模糊搜索,这可以给我下面的输出。这两个列表如下:

list_of_keys=['azure', 'job', 'matlab', 'javascript', 'http', 'android', 'amazon', 'apache spark'] 


result=['apache http server', 'angularjs', 'azure bot service', 'amazon s3', 'android sdk', 'android studio', 'amazon cloudfront']

所需输出:

简历中的 1 个单词:apache spark,JSON 数据中的单词:apache http 服务器

简历中的 2 个单词:amazon,JSON 数据中的单词:amazon s3,amazon cloudfront

简历中的 3 个单词:http,JSON 数据中的单词:apache http 服务器

我正在寻找一种模糊的搜索方法。我想要的是两个列表中是否有任何匹配,例如,如果“apache spark”与结果列表“apache http server”匹配,那么它应该作为输出打印:来自简历的单词:apache spark,来自 JSON 数据的单词:apache http server . 同样,如果 amazon 匹配,那么它应该打印为输出:简历中的单词:amazon,JSON 数据中的单词:amazon s3,amazon cloudfront,并且还显示分数/值。我一直在互联网上搜索,但找不到任何东西。您在这方面的帮助将不胜感激。

我使用了这种简单的方法:

for item in list_of_keys: 
    for item1 in result: 
        if item in item1: 
            print("Word from Resume: ", item, ", Word from JSON data: ", item1)
print ("****************\n")

我使用的第二种方法,但它没有从第二个列表中搜索任何内容:

以下代码适用于无法正常工作的 FuzzyWuzzy Search

def match_term(term, list_of_keys, min_score=0):
    # -1 score in case I don't get any matches
    max_score = -1
    
    # return empty for no match 
    max_name = ''
    
    # iterate over all names in the other
    for term2 in list_of_keys:
        # find the fuzzy match score
        score = fuzz.token_sort_ratio(term, term2)
    
        # checking if I am above my threshold and have a better score
        if (score > min_score) & (score > max_score):
            max_name = term2
            max_score = score
    return (max_name, max_score)


# list for dicts for easy dataframe creation
dict_list = []

#iterating over the sales file
for name in list_of_keys:
    #use the defined function above to find the best match, also set the threshold to a chosen #
    match = match_term(name, result, 94)
    
    #new dict for storing data
    dict_ = {}
    dict_.update({'Resume': name})
    dict_.update({'JSON Data': match[0]})
    dict_.update({'score': match[1]})
    
    dict_list.append(dict_)
print(dict_list)

标签: pythonstringlistkeywordfuzzywuzzy

解决方案


推荐阅读