【发布时间】:2021-05-23 11:48:14
【问题描述】:
我正在从事产品包创建和推荐项目。捆绑和推荐必须根据用户输入实时进行。
条件是 1.产品包应尽可能覆盖用户输入。 2. 推荐项目中用户输入的重复应该更少。
user_input=['a','b','c']
d1=['a','c','d']
d2=['a','b','e','f']
d3=['a','c','b','f']
d4=['b']
d5=['g','e','a']
d6=['g']
expected output - d1 + d4, d3, d1+d2,d5+d4+d1
以下是我的代码,它给出了结果,但显示了重复的结果,也没有显示所有组合。任何帮助表示赞赏。
dlist=[d1,d2,d3,d4,d5,d6]
diff_match=[]
# find match n diff of each product based on user input
for i in range(len(dlist)):
match=set(user_input).intersection(set(dlist[i]))
#print("match is",match)
diff=set(user_input).difference(set(dlist[i]))
#print("diff is",diff)
temp={'match':match,'diff':diff}
diff_match.append(temp)
for i in range(len(diff_match)):
# if match is found, recommend the product alone
diff_match_temp=diff_match[i]['match']
print("diff match temp is",diff_match_temp)
if diff_match_temp==user_input:
print ("absolute match")
#scenario where the user input is subset of product features, seperate from partial match
elif (all(x in list(diff_match_temp) for x in list(user_input))):
print("User input subset of product features")
print("The parent list is",diff_match[i]['match'])
print("the product is", dlist[i])
else:
'''else check if the difference between user input and the current product is fulfilled by other product,
if yes, these products are bundled together'''
for j in range(len(diff_match)):
temp_diff=diff_match[i]['diff']
print("temp_diff is",temp_diff)
# empty set should be explicitly checked to avoid wrong match
if (temp_diff.intersection(diff_match[j]['match'])==temp_diff and len(temp_diff)!=0 and list(temp_diff) != user_input) :
#if temp_diff==diff_match[j]['match'] and len(temp_diff)!=0 and list(temp_diff) != user_input :
print("match found with another product")
print("parent is",dlist[i])
print("the combination is",dlist[j] )
【问题讨论】:
标签: python python-3.x list recursion recommendation-engine