8. ディクショナリ critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 'The Night Listener': 3.0}}
11. さっきのディクショナリは? critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 'The Night Listener': 3.0}} 入れ子になってる!
14. データを表示させてみよう [email_address] :~$ python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >> from recommendations import critics >> critics['Lisa Rose']['Lady in the Water'] 2.5 >> critics['Toby']['Snakes on a Plane']=4.5 >> critics['Toby'] {'Snakes on a Plane':4.5,'Superman Returns':4.0,'You,Me and Dupree':1.0}
23. from math import sqrt #person1 と person2 の距離を基に類似スコアを返す def sim_distance(prefs,person1,person2): # 二人共評価しているアイテムのリストを得る si={} for item in prefs[person1]: if item in prefs[person2]: si[item]=1 # 両者ともに評価しているものが一つもなければ 0 を返す if len(si)==0: return 0 # すべての差の平方を足し合わせる sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2) for item in prefs[person1] if item in prefs[person2]]) return 1/(1+sum_of_squares)