首页 > 解决方案 > 使用字典构建匹配系统

问题描述

我正在尝试构建一种算法来匹配具有相同兴趣行业的人。他们共同的行业越多,他们就越有可能被推荐给彼此作为朋友。

我尝试过使用嵌套的 for 循环来解决它,但它有点复杂。这个想法是计算对方有多少次有类似的兴趣。例如,以 Jim 为对象,它应该数 Tony 1 次,Mark 3 次,Jessie 2 次。因此,它会推荐马克。

Interested_industry = {
    'Jim' : ['Technology','Automotive','Education', 'Environment'],
    'Tony': ['Food & Beverage','Automotive','Insurance', 'Tourism'],
    'Mark': ['Technology','Real Estate','Education', 'Environment'],
    'Jessie' : ['Technology','Real Estate','Transportation', 'Environment']
}

def match_friends():
    n_keys = len(Interested_industry.keys())
    for i in range(n_keys):
        n_values = len(Interested_industry.values())
        student =  list(Interested_industry.keys())[i]
    for j in range(n_values):
      for k in range(1, n_keys):
        student2 = list(Interested_industry.keys())[k]
    if  Interested_industry[student][j] in Interested_industry[student2]:
        print(student2)

match_friends()

更新感谢@martineau 的建议。

from itertools import permutations
def match_friends(interest_dict):
    for person1, person2 in permutations(interest_dict.keys(), 2):
        interests1, interests2 = interest_dict[person1], interest_dict[person2]
        common_interests = list(interests1 & interests2)
        print(f'{person1} & {person2}: common interests: '
              f'{len(common_interests)}')

industry_interests = {
    'Jim' : {'Technology','Automotive','Education', 'Environment'},
    'Tony': {'Food & Beverage','Automotive','Insurance', 'Tourism'},
    'Mark': {'Technology','Real Estate','Education', 'Environment'},
    'Jessie' : {'Technology','Real Estate','Transportation', 'Environment'}
}

match_friends(industry_interests)
Jim & Tony: common interests: 1
Jim & Mark: common interests: 3
Jim & Jessie: common interests: 2
Tony & Jim: common interests: 1
Tony & Mark: common interests: 0
Tony & Jessie: common interests: 0
Mark & Jim: common interests: 3
Mark & Tony: common interests: 0
Mark & Jessie: common interests: 3
Jessie & Jim: common interests: 2

后续问题:

我调整了代码,让它显示他们有多少共同兴趣。如果我想挑选具有最高共同兴趣的人进行配对。我如何过滤掉,例如,对于 Jim,他应该与 Mark 配对;马克应该与吉姆和杰西配对。所以返回应该像“Jim 与 Mark 配对”,“Mark 与 Jessie 配对”

标签: pythonpython-3.x

解决方案


我不了解您要完成的所有工作,但认为以下内容会有所帮助。它使用一个字典,其值是集合,它允许使用集合交集运算符“ &”确定共同兴趣。

它还将字典传递给match_friends()函数以避免使用全局变量,这通常被认为是一种不好的做法。

from itertools import permutations

def match_friends(interest_dict):
    for person1, person2 in permutations(interest_dict.keys(), 2):
        interests1, interests2 = interest_dict[person1], interest_dict[person2]
        common_interests = list(interests1 & interests2)
        print(f'{person1} & {person2}: common interests: '
              f'{common_interests if common_interests else "~"}')

industry_interests = {
    'Jim' : {'Technology','Automotive','Education', 'Environment'},
    'Tony': {'Food & Beverage','Automotive','Insurance', 'Tourism'},
    'Mark': {'Technology','Real Estate','Education', 'Environment'},
    'Jessie' : {'Technology','Real Estate','Transportation', 'Environment'}
}

match_friends(industry_interests)

输出:

Jim & Tony: common interests: ['Automotive']
Jim & Mark: common interests: ['Technology', 'Education', 'Environment']
Jim & Jessie: common interests: ['Environment', 'Technology']
Tony & Mark: common interests: ~
Tony & Jessie: common interests: ~
Mark & Jessie: common interests: ['Real Estate', 'Environment', 'Technology']

推荐阅读