请帮忙。我有一个包含 4 列(userid、movieid、score、timestamp)的数据文件,如下所示:196 242 3 881250949186 302 3 89171774222 377 1 878887116196 51 2 88060692362 257 2 879372434我正在尝试创建一个看起来像这样的嵌套字典:用户 = {'196': [('242', '3'), ('51', '2')], '186': ['302','3'] ...}我的代码只为每个用户 ID 选取一个元组 (movieid, score):def create_users_dict(): try: users = {} for line in open('u.data'): (id, movieid, rating, timestamp) = line.split('\t')[0:4] users[id] = (movieid, rating) except IOError as ioerr: print('There is an error with the file:' + str(ioerr)) return usersusers = create_users_dict()用户 = {'196': ('51', '2'), '186': ('302', '3')...}
添加回答
举报
0/150
提交
取消