Py学习  »  Python

使用索引运算符时python字典读取错误

Telkar Likhitha • 4 年前 • 275 次点击  

起初,我试着把我的数据集读进字典。

with open("msong.csv") as f:
reader = csv.DictReader(f)
data = [r for r in reader]

字典包含如下数据:

[{'': '0',
  'artist_name': 'Jack Johnson',
  'listen_count': '1',
  'release': 'Thicker Than Water',
  'song_id': 'SOAKIMP12A8C130995',
  'title': 'The Cove',
  'user_id': 'b80344d063b5ccb3212f76538f3d9e43d87dca9e',
  'year': '0'},
 {'': '1',
  'artist_name': 'Paco De Lucia',
  'listen_count': '2',
  'release': 'Flamenco Para Ni\xc3\xb1os',
  'song_id': 'SOBBMDR12A8C13253B',
  'title': 'Entre Dos Aguas',
  'user_id': 'b80344d063b5ccb3212f76538f3d9e43d87dca9e',
  'year': '1976'},

我使用这个功能根据用户的喜好推荐歌曲


def recommend(person, bound, similarity=pearson_similarity):
    scores = [(similarity(person, other), other) for other in data if other != person]

    scores.sort()
    scores.reverse()
    scores = scores[0:bound]

    print (scores)

    recomms = {}

    for sim, other in scores:
        ranked = data[other]

        for itm in ranked:
            if itm not in data[person]:
                weight = sim * ranked[itm]

                if itm in recomms:
                    s, weights = recomms[itm]
                    recomms[itm] = (s + sim, weights + [weight])
                else:
                    recomms[itm] = (sim, [weight])

    for r in recomms:
        sim, item = recomms[r]
        recomms[r] = sum(item) / sim

    return recomms

在提供输入时,我得到一个错误

recommend(data[0][user_id],4,euclidean_similarity)

错误:建议(数据[0][user_id],4,欧几里得相似性)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/37918
 
275 次点击  
文章 [ 1 ]  |  最新文章 4 年前