我完全不明白为什么我总是出错。我试图用sorted()
按字母顺序打印列出的书籍的标题
我不断地发现这个错误:
sorted(BSI, key=list(Book))
TypeError: 'type' object is not iterable
这就是代码
from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
BSI = [Book("J.K. Rowling", "Harry Potter", "Fantasy", "2005", 12.00, "34"),
Book("Dr. Seuss", "Green Eggs and Ham", "Children's", "2000", 8.00, "12"),
Book("Margaret Mitchell", "Gone with the Wind", "Fiction", "1980", 9.00, "30"),
Book("John Green", "The Fault in our Stars", "Fiction", "2010", 13.00, "23"),
Book("Stephanie Meyer", "Twilight", "Fantasy", "2008", 15.00, "8"),
Book("Suzanne Collins", "The Hunger Games", "Fantasy", "2005", 17.00, "18")]
for x in BSI:
print(x.title)
y = BSI
for x in BSI:
sorted(BSI, key=list(Book))
问题在于
list(Book)
。书是一种类型。以下可能是您想要的如果你真的认为你可能有重复的标题,你可以详细说明关键
相关问题 更多 >
编程相关推荐