nested dictionary with list of tuples as values from DATA file in python
up vote
1
down vote
favorite
Please, help. I have a data file with 4 columns (userid, movieid, score, timestamp) that looks like this:
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'] ...
My code only picks up one tuple (movieid, score) for each userid:
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')...
python
add a comment |
up vote
1
down vote
favorite
Please, help. I have a data file with 4 columns (userid, movieid, score, timestamp) that looks like this:
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'] ...
My code only picks up one tuple (movieid, score) for each userid:
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')...
python
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Please, help. I have a data file with 4 columns (userid, movieid, score, timestamp) that looks like this:
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'] ...
My code only picks up one tuple (movieid, score) for each userid:
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')...
python
Please, help. I have a data file with 4 columns (userid, movieid, score, timestamp) that looks like this:
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'] ...
My code only picks up one tuple (movieid, score) for each userid:
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')...
python
python
edited Nov 10 at 17:36
Daniel Mesejo
7,9841922
7,9841922
asked Nov 10 at 17:24
LMHull
426
426
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Use 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.
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.
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Use 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.
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.
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
add a comment |
up vote
1
down vote
accepted
Use 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.
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.
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use 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.
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.
Use 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.
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.
edited Nov 10 at 17:39
answered Nov 10 at 17:34
Daniel Mesejo
7,9841922
7,9841922
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
add a comment |
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
Fantastic! Very much thank you!!!
– LMHull
Nov 10 at 18:41
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241535%2fnested-dictionary-with-list-of-tuples-as-values-from-data-file-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown