I am trying to combine two lists and place them into a dictionary. Here is my current code:
from string import*
book_ratings=open("ratings.txt","r")
usernames=[]
ratings=[]
for i in book_ratings.readlines(): #i=index
i=split(i) #splits usernames from ratings
rating=i[:] #allows whole ratings list to be stored in ratings
ratings.append(rating) #appends rating to ratings list
usernames.append(i) #appends usernames to usernames list
books=open("books.txt","r")
book_list=[]
i=books.readlines()
book_list.append(i) #appends books to book_list list
user_book_ratings={}
for ratings in book_ratings:
user_book_ratings.join(book_list,ratings)
Unfortunately, the last three lines only print out an empty list. How do I combine the ratings and book_list lists, and place them in a dictionary with usernames as keys and the new list as values?
Thank you in advance.
*edit
The file ratings.txt appears as follows:
"Ben 5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 Moose 5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0..." (There are more users, these are the first two)
When printed with the above code, appears as:
"[['Ben'], ['5', '0', '0', '0', '0', '0', '0', '1', '0', '1', '-3', '5', '0', '0', '0', '5', '5', '0', '0', '0', '0', '5', '0', '0', '0', '0', '0', '0', '0', '0', '1', '3', '0', '1', '0', '-5', '0', '0', '5', '5', '0', '5', '5', '5', '0', '5', '5', '0', '0', '0', '5', '5', '5', '5', '-5'], ['Moose'], ['5', '5', '0', '0', '0', '0', '3', '0', '0', '1', '0', '5', '3', '0', '5', '0', '3', '3', '5', '0', '0', '0', '0', '0', '5', '0', '0', '0', '0', '0', '3', '5', '0', '0', '0', '0', '0', '5', '-3', '0', '0', '0', '5', '0', '0', '0', '0', '0', '0', '5', '5', '0', '3', '0', '0']..."
The file books.txt appears as follows:
Douglas Adams,The Hitchhiker's Guide To The Galaxy
Richard Adams,Watership Down
Mitch Albom,The Five People You Meet in Heaven
Laurie Halse Anderson,Speak
Maya Angelou,I Know Why the Caged Bird Sings
... (There are more books; these are the first five)
When printed with the above code, looks like:
"[["Douglas Adams,The Hitchhiker's Guide To The Galaxy\n", 'Richard Adams,Watership Down\n', 'Mitch Albom,The Five People You Meet in Heaven\n', 'Laurie Halse Anderson,Speak\n', 'Maya Angelou,I Know Why the Caged Bird Sings\n'..."
Ultimately, the dictionary should appear as:
{Ben:["Douglas Adams,The Hitchhiker's Guide to The Galaxy","5"],["Richard Adams,Watership Down","0"]... Moose:["Douglas Adams, The Hitchhiker's Guide to the Galaxy","5"]...} and so on (or something similar to that).
The idea is that each book is assigned a rating by the user in question.
I hope I've made things clearer.