Compare two files and make a list

I have two files that I want to compare with each other and form a list. Each file have their own class. Book and Person. In these, I have different attributes. The ones I want to compare are: person.personalcode == book.borrowed. From this I want a list of all the borrowed books. I have started like this:

for person in person_list:
    for book in booklibrary_list:
        if person.personalcode == book.borrowed:
            person.books.append(book, person)

for person in person_list:
    if len(person.books) > 0:
        print(person.personalcode + "," + person.firstname + person.lastname + "have borrowed the following books: ")
        for book in person.books:
            print(book)

for person in person_list:
    person.books = []

But it does not work, what have I missed or done wrong?

Answers


Posting as an answer as this is too long for a comment.

First: improve your question. Show how you construct the Person and the Book class, and how you populate them. Describe what the personalcode is and how come personalcode would be the same as a book code. Some sample data and a bit more code would make this easier to answer.

Second: reading your other question, you seem to be storing your data in a text file, loading and querying, modifying and saving the data directly. This will lead you to problems and instead you should consider going down one of two lines:

  1. Use an SQL database, possibly the easiest to start with is SQLite as it does not need a server to be set up and there is a module in the standard library that is very easy to use. Store your data there and you will find it easier in the long run.
  2. Use Python objects (e.g. three classes: Person, Book, and BorrowedBook), manage lists of them within the program, and use shelve from the standard library to store and retrieve these lists of objects between queries.

The use of shelve would be easier if you have not used SQL before, and I hope you will forgive the pun when I say that it might be very appropriate for a book-related application!


Need Your Help

Why does reducing this lazy sequence slow down this Clojure program 20x?

clojure fibonacci reduce lazy-sequences take

I have a Clojure program that returns the sum of a lazy sequence of even Fibonacci numbers below n: