我是python的新手,我正在尝试检查每个用户的两个不同嵌套字典的书是否相同;如果相同,每本书的旧用户评分将乘以每本书的新用户评分,相应地对应于每本书。然后,我要添加为每个老用户乘以的所有图书评分。
这就是我想要做的:
books = ['book1', 'book2', 'book3','book4','book5','book6','book7','book8','book9','book10']
names = ['anas', 'jana', 'mo', 'farhan']
biglist = [{'anas':{'book1':0,'book2':-2,'book3':3,'book4':3,'book5':3,'book6':5,'book7':4,'book8':0,'book9':2,'book10':2}},
{'jana':{'book1': 0,'book2':3,'book3':-3,'book4':2,'book5':4,'book6':0,'book7':-5,'book8':2,'book9':0,'book10':-3}},
{'mo':{'book1': 0,'book2': 5,'book3': 3,'book4':3,'book5':3,'book6':2,'book7':2,'book8':3,'book9':5,'book10':-1}},
{'farhan':{'book1':2,'book2':4,'book3':0,'book4':0,'book5':1,'book6':-5,'book7':0,'book8':1,'book9':-3,'book10':-2}}]
new_ratings = []
global new_name_list
new_name_list = []
shuffle = random.sample(books,3)
small_list = []
#THE USER IS GOING TO RATE 3 BOOKS
global z
z = {}
def cool():
global new_name
new_name = input("name ")
new_name_list.append(new_name)
for i in range(3):
books_buff = books[:]
x = shuffle[i]
print(x)
user_rating = int(input(""))
if user_rating != 0 and x in books_buff:
books.remove(x)
new_ratings.append(user_rating)
global smalldict
smalldict = dict(zip(shuffle,new_ratings))
z[new_name] = smalldict
small_list.append(z)
print(small_list)
def multi():
result=[]
for l in small_list:
# print(l)
for k, v in l.items():
global k2
global v2
for k2 , v2 in v.items():
# print(k2,v2)
print(k2, v2)
for list_item in biglist:
# print(list_item)
for key, value in list_item.items():
global key2
global value2
for key2, value2 in value.items():
# print(key2,value2)
pass
if k2 in v.items() == key2 in value.items():
print(k2)
for ratings1, ratings2 in zip(v2,value2):
result.append(ratings1*ratings2)
print(result)
但是作为输出,我得到了一个空字典。
例如,如果新用户对这三本书的评分如下:
book1
1
book2
3
book9
-5
我将其作为所需的输出:
{'anas':{'book1':0,'book2':-6,'book9':-10}}
{'jana':{'book1':0,'book2':9,'book9':0}}
{'mo':{'book1':0,'book2':15,'book9':-25}}
{'farhan':{'book1':2,'book2':12,'book9':15}}
[-16,9,-10,29]