0

In Python, is there a function that classifies and orders an array of objects by an attribute?

Example:

class Book:
    """A Book class"""
    def __init__(self,name,author,year):
        self.name = name
        self.author = author
        self.year = year

hp1 = Book("Harry Potter and the Philosopher's stone","J.k Rowling",1997)
hp2 = Book("Harry Potter and the chamber of secretse","J.k Rowling",1998)
hp3 = Book("Harry Potter and the Prisioner of Azkaban","J.k Rowling",1999)

#asoiaf stands for A Song of Ice and Fire
asoiaf1 = Book("A Game of Thrones","George R.R Martin",1996)
asoiaf2 = Book("A Clash of Kings","George R.R Martin",1998)

hg1 = Book("The Hunger Games","Suzanne Collins",2008)
hg2 = Book("Catching Fire","Suzanne Collins",2009)
hg3 = Book("Mockingjaye","Suzanne Collins",2010);

books = [hp3,asoiaf1,hp1,hg1,hg2,hp2,asoiaf2,hg3]
#disordered on purpose

organized_by_autor = magic_organize_function(books,"author")

Does the magic_organize_function exist? Otherwise, what would it be?

8
  • So do you want to group-by and sort by author? Commented Dec 20, 2016 at 21:02
  • 3
    The sorted() function in Python takes a key parameter. It is used to specify how you want your list sorted. In your case you want to sort by the author attribute of each element in the list books: organized_by_autor = sorted(books, lambda book: book.author) Commented Dec 20, 2016 at 21:02
  • FWIW the ; are superfluous. They don't do anything. Commented Dec 20, 2016 at 21:04
  • If the linked question doesn't solve your problem, let me know. Commented Dec 20, 2016 at 21:04
  • If the duplicate that @WayneWerner linked to is still not clear, I suggest taking at look at this post. Commented Dec 20, 2016 at 21:07

1 Answer 1

1

Sorting by author is one way:

sorted_by_author = sorted(books, key=lambda x: x.author)
for book in sorted_by_author:
    print(book.author)

Output:

George R.R Martin
George R.R Martin
J.k Rowling
J.k Rowling
J.k Rowling
Suzanne Collins
Suzanne Collins
Suzanne Collins

You can also nicely group by author using itertools.groupby:

from itertools import groupby

organized_by_author = groupby(sorted_by_author, key=lambda x: x.author)

for author, book_from_author in organized_by_author:
    print(author)
    for book in book_from_author:
        print('    ', book.name)

Output:

George R.R Martin
     A Game of Thrones
     A Clash of Kings
J.k Rowling
     Harry Potter and the Prisioner of Azkaban
     Harry Potter and the Philosopher's stone
     Harry Potter and the chamber of secretse
Suzanne Collins
     The Hunger Games
     Catching Fire
     Mockingjaye

Note: You need to feed groupby the sorted sorted_by_author.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for. Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.