用Python构建词典匹配系统
我正在尝试建立一个算法来匹配具有相同兴趣行业的人。他们的共同行业越相似,就越有可能被彼此推荐为朋友 我是Python的初学者,有人知道怎么做吗 我尝试过使用嵌套for循环来解决它,但它有点复杂。这样做的目的是计算另一个人有多少次类似的兴趣。例如,以Jim为对象,它应该计算Tony 1次,Mark 3次,Jessie 2次。因此,它将推荐Mark 谢谢你阅读我的问题用Python构建词典匹配系统,python,python-3.x,Python,Python 3.x,我正在尝试建立一个算法来匹配具有相同兴趣行业的人。他们的共同行业越相似,就越有可能被彼此推荐为朋友 我是Python的初学者,有人知道怎么做吗 我尝试过使用嵌套for循环来解决它,但它有点复杂。这样做的目的是计算另一个人有多少次类似的兴趣。例如,以Jim为对象,它应该计算Tony 1次,Mark 3次,Jessie 2次。因此,它将推荐Mark 谢谢你阅读我的问题 Interested_industry = { 'Jim' : ['Technology','Automotive','Ed
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)
我调整代码,让它显示他们有多少共同的兴趣。如果我想和最有共同兴趣的人结对。例如,对于吉姆来说,他应该和马克配对;马克应该与吉姆和杰西配对。所以回报应该像“吉姆和马克配对”,“马克和杰西配对”
非常感谢你 我不理解你想要完成的每一件事,但我认为以下几点会有所帮助。它使用一个值为集合的字典,允许使用集合交集运算符“
&
”确定共同兴趣
它还将字典传递给match\u 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)
输出:
吉姆和托尼:共同的兴趣:[“汽车”]
吉姆和马克:共同兴趣:[“技术”、“教育”、“环境”]
吉姆和杰西:共同的兴趣:[“环境”、“技术”]
托尼和马克:共同利益:~
托尼和杰西:共同利益:~
马克和杰西:共同利益:[“房地产”、“环境”、“技术”]
我不理解您试图完成的所有任务,但我认为以下内容会有所帮助。它使用一个值为集合的字典,允许使用集合交集运算符“&
”确定共同兴趣
它还将字典传递给match\u 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)
输出:
吉姆和托尼:共同的兴趣:[“汽车”]
吉姆和马克:共同兴趣:[“技术”、“教育”、“环境”]
吉姆和杰西:共同的兴趣:[“环境”、“技术”]
托尼和马克:共同利益:~
托尼和杰西:共同利益:~
马克和杰西:共同利益:[“房地产”、“环境”、“技术”]
您必须使用字典才能完成此操作?不,如果有其他解决方案,我愿意尝试。我现在只知道字典,哈哈。好吧,首先,如果你要迭代字典,不要这样做“list(interest\u industry.keys())[I]
。这使得您的迭代时间是二次的。线性空间是二次的。只需使用作为感兴趣的行业的关键:
…python支持集合相交。您必须使用字典来实现这一点吗?不,如果有其他解决方案,我很乐意尝试。我现在只知道字典哈哈。好吧,对于初学者来说,如果您打算迭代dic当然,不要这样做“list(interest\u industry.keys())[i]
。这使得您的迭代时间是二次的。和线性空间。只需使用了解感兴趣的行业:
…python支持set Intersection谢谢,它绝对有帮助。置换函数确实解决了我面临的问题。非常感谢你!我还有一个关于过滤退货的问题,如果你能看一下就好了,非常感谢。吉姆:对不起,stackoverflow不是那样工作的(它不是免费的编码服务)。如果你还有一个非琐碎的问题,你需要把你自己试图解决的问题作为一个单独的问题发布出来。还要注意的是,你需要考虑当有共同利益的数量达到一定程度时会发生什么,并澄清你所说的“结对”在产出或结果方面的确切含义。谢谢,这肯定会有所帮助。置换函数确实解决了我面临的问题。非常感谢你!我还有一个关于过滤退货的问题,如果你能看一下就好了,非常感谢。吉姆:对不起,stackoverflow不是那样工作的(它不是免费的编码服务)。如果你还有一个非琐碎的问题,你需要把你自己试图解决的问题作为一个单独的问题发布出来。还要注意的是,你需要考虑当共同利益的数量达到一定程度时会发生什么,并明确你所说的“配对”在产出或结果方面的确切含义。