我正在研究一个项目,该项目是一种图书推荐算法,该算法通过将用户输入乘以该图书的相应用户评分来推荐图书,然后将所有用户的评分加起来。
biglist = [{'user1' : {'book1' : 0, 'book2' : -2, 'book3' : 3}},
{'user2' : {'book1' : 0, 'book2' : 3, 'book3' : -3,}},
{'user3' : {'book1' : 0, 'book2' : 5, 'book3' : 3}},
{'user4': {'book1':2, 'book2':4, 'book3': 0}}]
现在,我可以打印所有结果的乘法,但是我在为每个用户求和时遇到了麻烦,这是我尝试过的操作
global new_ratings
new_ratings=[]
shuffle = random.sample(books,3)
def ratings():
multi = []
tmp = []
isk = []
global new_user_name
new_user_name=input("enter your name ")
new_names.append(new_user_name)
global books_buff
books_buff = books[:]
for list_item in biglist:
for key, value in list_item.items():
global key2
global value2
for key2, value2 in value.items():
templist.append(value2)
for i in range(3):
x = shuffle[i]
print(x)
user_rating = int(input(""))
new_ratings.append(user_rating)
for l in biglist:
for key,value in l.items():
for key2,value2 in value.items():
if user_rating != 0 and x == key2 :
multi = user_rating * value2
isk.append(multi)
fa = {}
fa[key2] = multi
print(multi)
tmp.append(fa)
print(tmp)
print(isk)
当用户输入2时,这是我想要的输出,例如:
book1:
2
book2:
2
book3:
2
[{'user1':{'book1':0,'book2':-4,'book3':6,'sum':2}},{'user2':{'book1':0,'book2':6,'book3':-6,'sum':0}},{'user3':{'book1':0,'book2':10,'book3':6,'sum':16}},{'user4':{'book1':4,'book2':8,'book3':0, 'sum':12}}]
我不确定您的代码对所有全局变量和改组做了什么,但是您所描述的任务(至少我认为这是您所描述的,基于给定的输出)通过一些简单的理解语句即可完成。
from typing import Dict, List
def scaled_ratings(
biglist: List[Dict[str, Dict[str, int]]]
) -> List[Dict[str, Dict[str, int]]]:
"""
Given a biglist of {user: {book: rating, book: rating}} dicts,
prompt the user for a scaling factor to apply to the rating for each
book, and return a new version of the list with all the ratings multiplied
by the appropriate scaling factor, along with the sum of each user's ratings.
"""
# First get the set of all books from biglist.
all_books = sorted(list({
b for d in biglist for r in d.values() for b in r.keys()
}))
# Now get the scaling factor for each book from the user.
scale = {
book: int(input(f"{book}:\n"))
for book in all_books
}
# Now build a modified version of biglist with all the ratings scaled.
scaled_ratings = [{user: {
book: rating * scale[book]
for book, rating in ratings.items()
} for user, ratings in d.items()} for d in biglist]
# Now add the sum into each ratings dict.
for d in scaled_ratings:
for ratings in d.values():
ratings['sum'] = sum(ratings.values())
return scaled_ratings
biglist = [
{'user1': {'book1': 0, 'book2': -2, 'book3': 3}},
{'user2': {'book1': 0, 'book2': 3, 'book3': -3}},
{'user3': {'book1': 0, 'book2': 5, 'book3': 3}},
{'user4': {'book1': 2, 'book2': 4, 'book3': 0}}
]
print(scaled_ratings(biglist))
这是您可以做的:
r1 = int(input("Book1: "))
r2 = int(input("Book2: "))
r3 = int(input("Book3: "))
rs = [r1,r2,r3]
biglist = [ {'user1' : {'book1' : 0, 'book2' : -2, 'book3' : 3}},
{'user2' : {'book1' : 0, 'book2' : 3, 'book3' : -3,}},
{'user3' : {'book1' : 0, 'book2' : 5, 'book3' : 3}},
{'user4' : {'book1':2, 'book2':4, 'book3': 0}} ]
for d in biglist:
s = 0
for v in d.values():
for k,r in zip(v.keys(),rs):
v.update({k:v[k]*r})
s += v[k]
v.update({'sum':s})
print(biglist)
输入:
Book1: 2
Book2: 2
Book3: 2
输出:
[{'user1': {'book1': 0, 'book2': -4, 'book3': 6, 'sum': 2}}, {'user2': {'book1': 0, 'book2': 6, 'book3': -6, 'sum': 0}}, {'user3': {'book1': 0, 'book2': 10, 'book3': 6, 'sum': 16}}, {'user4': {'book1': 4, 'book2': 8, 'book3': 0, 'sum': 12}}]