python - 使用字典构建匹配系统
问题描述
我正在尝试构建一种算法来匹配具有相同兴趣行业的人。他们共同的行业越多,他们就越有可能被推荐给彼此作为朋友。
我尝试过使用嵌套的 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 配对”
解决方案
我不了解您要完成的所有工作,但认为以下内容会有所帮助。它使用一个字典,其值是集合,它允许使用集合交集运算符“ &
”确定共同兴趣。
它还将字典传递给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']
推荐阅读
- python - 使用 Django bulk_create() 函数时,“dict”对象没有属性“pk”
- php - PHP - 不贪婪的正则表达式仍然有点“贪婪”
- sql - 尝试复式记账系统,但获取交易清单是个问题
- r - 使用 ggcorrplot 包更改相关图对角线的方向
- c++ - delete[] (ptr, 0) 的行为
- python - Python,for循环用于打破重复
- highcharts - Highchart-graph 没有出现在 Internet Explorer 中
- neo4j - Neo4j最短路径查询算法的连续关系约束如何表达?
- wordpress - 如何根据填写的结帐页面表格申请折扣
- json - 尝试使用 ruby 和 json 动态填充 infoblox 域名