0

Python noob here so please bear with me! I have a list that looks like this:

bookList = [("Wuthering Heights", "fred"), ("Everville", "fred"), ("Wuthering Heights", "dan")]

What I’m trying to do is write a function that looks at each nested list and sees who shares books in common with who, depending who is logged in. For example, if dan was logged in, the system would say “fred also has plums”.

I have a dictionary set up the holds usernames as keys and passwords as their value.

I’m kind of struggling with list comprehension when they involve anything nested, and help would be greatly appreciated!

1
  • Can you provide an example of output? I mean, if that is the bookList and search(username, bookList) is the function, what should be the result of search('fred', bookList) and search('dan', bookList)? Also I don't get what's the relation of your dictionary with usernames as keys with this... Commented Mar 21, 2013 at 19:28

3 Answers 3

2

I don't think your existing data structure is really ideal for this. What I would do would be to pre-process it into a dictionary whose keys are the usernames and the values are sets of books. Then you can do a loop or list comprehension to compare the logged-in user with all the other users and see if there is anything in common. So:

from collections import defaultdict
bookdict = defaultdict(set)
for book, name in bookList:
    bookdict[name].add(book)
logged_in_user = 'fred'
for person, books in bookdict.items():
    if person == logged_in_user:
        continue
    common = books.intersection(bookdict[logged_in_user])
    if common:
        print '%s also has %s' % (person, ', '.join(common))
Sign up to request clarification or add additional context in comments.

Comments

0
def common_books(user):
    user_books = {b for b, u in bookList if u == user}
    for b, u in bookList:
        if b in user_books and u != user:
            print '{0} also has {1}'.format(u,b)

Comments

-1

If you're trying to get the books that fred has in the list

filter(lambda x: x[1] == "fred", bookList)

Another version as per Bakuriu's comment.

class Session:
    def __init__(self):
        self.books = ["Wuthering Heights", "Everville"]
        self.username = "fred"

bookList = [("Wuthering Heights", "fred"), ("Everville", "fred"), ("Wuthering Heights", "dan")]

if __name__ == "__main__":

    session = Session()

    for book in bookList:
        if book[1] != session.username and book[0] in session.books:
            print "{} also has {}".format(book[1], book[0])

2 Comments

I belive he wants, given a username, e.g. dan,, get all the names of users which have a book in common with dan. Or at least this is how I'd interpret the sentence: For example, if dan was logged in, the system would say “fred also has plums”.
Yes Bakuriu that's correct:) Sorry, my explanation is not very clear!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.