2012-03-14 56 views
0

我正在學習如何智力的集體作品,我在練習中第2章這裏做例子recommendations.py問題是鏈接:Python和集體智慧:第2章:推薦項目

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 

這是我收到的信息。我不確定我是否正確地複製並粘貼了代碼,或者如果給定的代碼行是錯誤的。

回答

2

這...

totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim 

,就是要兩條線:

totals.setdefault(item,0) 
totals[item]+=prefs[other][item]*sim 
+0

謝謝。我看了這本書的pdf版本,它顯示了兩行。 – tanaks1789 2012-03-14 08:50:50