我正在学习集体智慧是如何运作的,我正在练习第2章中的例子suggest.py问题。这是链接:
http://cdn.jampad.net/Library/collectiveintelligence/#calibre_link-201
当我复制并粘贴此代码时:
# Gets recommendations for a person by using a weighted average
# of every other user's rankings
def getRecommendations(prefs,person,similarity=sim_pearson):
totals={}
simSums={}
for other in prefs:
# don't compare me to myself
if other==person: continue
sim=similarity(prefs,person,other)
# ignore scores of zero or lower
if sim<=0: continue
for item in prefs[other]:
# only score movies I haven't seen yet
if item not in prefs[person] or prefs[person][item]==0:
# Similarity * Score
totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim
# Sum of similarities
simSums.setdefault(item,0)
simSums[item]+=sim
# Create the normalized list
rankings=[(total/simSums[item],item) for item,total in totals.items( )]
# Return the sorted list
rankings.sort( )
rankings.reverse( )
return rankings
进入我的recommendations.py文件,当我重新加载文件时,收到语法错误。
>>> reload(recommendations)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "recommendations.py", line 100
totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim
^
SyntaxError: invalid syntax
这是我收到的消息。我不确定我是否正确地复制和粘贴了代码,或者给定的代码行是否错误。
答案 0 :(得分:2)
此...
totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim
意味着两行:
totals.setdefault(item,0)
totals[item]+=prefs[other][item]*sim