[英]nested dictionary with list of tuples as values from DATA file in python
請幫忙。 我有一個包含 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
我正在嘗試創建一個看起來像這樣的嵌套字典:
用戶 = {'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 users
users = create_users_dict()
用戶 = {'196': ('51', '2'), '186': ('302', '3')...}
使用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)
輸出
{'196': [('242', '3'), ('51', '2')], '62': [('257', '2')], '186': [('302', '3')], '22': [('377', '1')]}
一種可能的替代方法是檢查鍵 ( 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))
作為旁注,您不應該使用id
作為名稱,因為它隱藏了內置函數id 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.