-1

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.

Community
  • 1
  • 1
user3052287
  • 61
  • 1
  • 6
  • 1
    I have no idea what you're trying to do in this code. There's a lot of stuff in there that makes me ask "why would you want to do that?". Please post the two lists that you would like to be joined into a dictionary, and what you would like that dictionary to look like, and I'm sure that we can get you the code you need for that – inspectorG4dget Mar 22 '14 at 22:45
  • Could you provide sample `ratings.txt` and `books.txt` content along with the desired output? – alecxe Mar 22 '14 at 22:45
  • 1
    possible duplicate of [Map two lists into a dictionary in Python](http://stackoverflow.com/questions/209840/map-two-lists-into-a-dictionary-in-python) – Cilyan Mar 22 '14 at 22:55

2 Answers2

0

Here is how to combine two lists:

def combine(listone, listtwo):
    out = {}
    for k in range(0, len(listone)):
        out[listone[k]] = listtwo[k]
    return out

This runs as:

>>> combine(['hello', 'is', 'a', 'word'], [5, 2, 1, 4])
{'a': 1, 'is': 2, 'word': 4, 'hello': 5}

By the way, those lists were basically getting the length of each string

  • It's considered poor form to overwrite global variables like `map`. Better to use a name like `out`. – gabe Mar 22 '14 at 23:25
  • The OP needs something a bit more complex than just having keys in one list and values in the other. Also, anytime you do `range(len(something))`, there's a good chance it can be simplified. In this case, you can condense the entire function to one line: `return dict(zip(listone, listtwo))`. – lvc Mar 22 '14 at 23:29
0

Get your booknames first, because you want to know those as you read the usernames - then you can put all the data straight into the final dict, with fewer intermediate lists. There's no need for any appending with this, since readlines already gives you a list - so, just do:

with open('books.txt', 'r') as books:
    book_titles = books.readlines()

using the with statement ensures that the file will be closed as soon as you're done reading from it.

Now that you have that, the way you get the usernames and ratings is basically the same as what you have, although it can be simplified a little:

with open('ratings.txt', 'r') as ratings:
   for line in ratings:
      username, *ratings = line.split()

This syntax tells Python to split the string and assign the first (left-most) component to username (as a string), and group the rest into a list and put in ratings.

The dictionary you want (you'll need to initialise it to an empty dict sometime before this loop) has the username as the key, and the value as a list of corresponding pairs from ratings and book_titles. Python has a builtin called zip that gets those pairs for you - although it doesn't give you a list of pairs in Python 3, so you need to pass the result through list:

ratings_dict = {}
with open('ratings.txt', 'r') as ratings:
   for line in ratings:
      username, *ratings = line.split()
      ratings_dict[username] = list(zip(book_titles, ratings))
lvc
  • 34,233
  • 10
  • 73
  • 98