在Python中,是否有一个函数可以按属性对对象数组进行分类和排序?你知道吗
示例:
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")
魔法组织功能存在吗?否则,会是什么?你知道吗
按作者排序是一种方法:
输出:
您还可以使用^{} 按作者很好地分组:
输出:
注意:您需要向
groupby
输入排序后的sorted_by_author
。你知道吗相关问题 更多 >
编程相关推荐