[英]nested dictionary with list of tuples as values from DATA file in python
Please, help.请帮忙。 I have a data file with 4 columns (userid, movieid, score, timestamp) that looks like this:
我有一个包含 4 列(userid、movieid、score、timestamp)的数据文件,如下所示:
196 242 3 881250949
186 302 3 891717742
22 377 1 878887116
196 51 2 880606923
62 257 2 879372434
I am trying to create a nested dictionary that should look like this:我正在尝试创建一个看起来像这样的嵌套字典:
users = {'196': [('242', '3'), ('51', '2')], '186': ['302','3'] ...}
用户 = {'196': [('242', '3'), ('51', '2')], '186': ['302','3'] ...}
My code only picks up one tuple (movieid, score) for each userid:我的代码只为每个用户 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 users
users = create_users_dict()
users = {'196': ('51', '2'), '186': ('302', '3')...}
用户 = {'196': ('51', '2'), '186': ('302', '3')...}
Usesetdefault :使用setdefault :
def create_users_dict():
try:
users = {}
for line in open('u.data'):
uid, movie_id, rating, timestamp = line.split()
users.setdefault(uid, []).append((movie_id, rating))
return users
except IOError as ioerr:
print('There is an error with the file:' + str(ioerr))
users = create_users_dict()
print(users)
Output输出
{'196': [('242', '3'), ('51', '2')], '62': [('257', '2')], '186': [('302', '3')], '22': [('377', '1')]}
A possible alternative is to check if the key ( uid
) is in the dictionary, in case is missing initialize the value with the empty list and then simply append.一种可能的替代方法是检查键 (
uid
) 是否在字典中,以防丢失用空列表初始化值,然后简单地追加。
def create_users_dict():
try:
users = {}
for line in open('u.dat'):
uid, movie_id, rating, timestamp = line.split()
if uid not in users:
users[uid] = []
users[uid].append((movie_id, rating))
return users
except IOError as ioerr:
print('There is an error with the file:' + str(ioerr))
As a side note you should not use id
as a name because it shadows the built-in function id .作为旁注,您不应该使用
id
作为名称,因为它隐藏了内置函数id 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.