from random import sample
import json
from math import sqrt
import time
from collections import defaultdict
#曼哈顿 欧式 cos 三种距离
users={"Angelica":{"星际穿越":3.5,“放牛班的春天”:2.0,
"肖申克的救赎":4.5,"盗梦空间":5.0,"阿甘正传":1.5,"乱世佳人":2.5,"傲慢与偏见":2.0},"Bill":{"星际穿越":2.0,"放牛班的春天":3.5,"肖申克的救赎":4.5,"盗梦空间":5.0,"阿甘正传":1.5,"乱世佳人":2.5,"傲慢与偏见":2.0},"Chan":{"星际穿越":2.0,"放牛班的春天":3.5,"肖申克的救赎":3.5,"盗梦空间":5.0,"阿甘正传":2.5,"乱世佳人":2.5,"傲慢与偏见":2.0}}defman_dis(usrl,usr2):
distance =0
flag=Falsefor m in users[usrl]:if m in users [usr2]:
distance +=abs(users[usr1][m]-users[usr2][m])#distance += (users[usr1][m]-users[usr2][m])**2
flag =Trueif flag ==True:return distance
else:return-1defau1_dis(usrl,usr2):
distance =0
flag=Falsefor m in users[usrl]:if m in users [usr2]:
distance +=(users[usr1][m]-users[usr2][m])**2
flag =Trueif flag ==True:return sqrt(distance)else:return-1defcos_dis(usrl,usr2):
ratingl=users[usr1]
rating2=users[usr2]
common_movies=[x for x in ratingl if x in rating2]iflen(common_movies)==0:return-1
fenzi=sum([ratingl[x]*rating2[x]for x in common_movies])
len_1=sqrt(sum([rating1[x]**2for x in common_movies]))
len_2=sqrt(sum([rating2[x]**2for x in common_movies]))if len_1 + len_2==0:return-1return fenzi/(len_1 * len_2)defau2_dis(usrl,usr2):
ratingl=users[usr1]
rating2=users[usr2]
common_movies=[x for x in ratingl if x in rating2]iflen(common_movies)==0:return-1return sqrt(sum([(ratingl[x]- rating2[x])**2for x in common_movies]))