简体   繁体   English

如何使用namedtuple的另一个列表追加到列表?

[英]How to append to a list using another list of namedtuples?

so I'm very new to Python and got stuck on a problem for my introductory CS class. 因此,我对Python还是很陌生,在入门CS课上遇到了一个问题。 The problem is to create aa list containing all titles created before 2000 and all titles created after 2000. This is what I have so far: 问题是要创建一个包含2000年之前创建的所有标题和2000年之后创建的所有标题的列表。这是我到目前为止的内容:

from collections import namedtuple
Book = namedtuple("Book", "author title genre year price instock")
book_1 = Book("Bob", "Harry Potter", "Fantasy", 2000, 6.00, 1000)
book_2 = Book("Martha", "Hunger Games", "Psychological", 1998, 10.00, 2000)
book_3 = Book("Sam", "The Quest", "Adventure", 2010, 8.00, 5000)
book_4 = Book("Damien", "Pokemon", "Sci-Fi", 1990, 12.00, 10000)
book_5 = Book("Voldemort", "Maze Runner", "Adventure", 2015, 10.00, 50)
book_6 = Book("Anonymous", "Horror Stories Before Bed", "Horror", 2017, 18.00,0)
book_store_inventory = [book_1, book_2, book_3, book_4, book_5, book_6]

before_2000 = []
after_2000 = []

for i in book_store_inventory:
    if book_store_inventory[i].year <= 2000:
        before_2000.append(i.title)
    else:
        after_2000.append(i.title)

What should I change around from this point? 从这一点上我应该改变什么? I keep getting error messages saying list indices must be integers or slices, not Book. 我不断收到错误消息,说列表索引必须是整数或切片,而不是Book。 Thanks! 谢谢!

you don't need an index: 您不需要索引:

for book in book_store_inventory:
    if book.year <= 2000:
        before_2000.append(book.title)
    else:
        after_2000.append(book.title)
before_2000 = [i.title for i in book_store_inventory if i.year <= 2000]

after_2000 = [i.title for i in book_store_inventory if i.year > 2000]

Since you have so many book objects, it may make sense to create a new class to store the books and create property decorators to access the library data based on the certain condition: 由于您有很多书籍对象,因此可以根据特定条件创建一个新类来存储书籍并创建property装饰器来访问库数据,这是很有意义的:

class Library:
   def __init__(self, books):
      self.books = books
   @property
   def before_2000(self):
      return [i for i in self.books if i.year <= 2000] 
   @property
   def after_2000(self):
      return [i for i in self.books if i.year > 2000]
   def __repr__(self):
       return '{}({})'.format(self.__class__.__name__, ', '.join(i.title for i in self.books))

book_store_inventory = [book_1, book_2, book_3, book_4, book_5, book_6]
library = Library(book_store_inventory)
print(library.before_2000)
print(library.after_2000)
print(library)

Output: 输出:

[Book(author='Bob', title='Harry Potter', genre='Fantasy', year=2000, price=6.0, instock=1000), Book(author='Martha', title='Hunger Games', genre='Psychological', year=1998, price=10.0, instock=2000), Book(author='Damien', title='Pokemon', genre='Sci-Fi', year=1990, price=12.0, instock=10000)]
[Book(author='Sam', title='The Quest', genre='Adventure', year=2010, price=8.0, instock=5000), Book(author='Voldemort', title='Maze Runner', genre='Adventure', year=2015, price=10.0, instock=50), Book(author='Anonymous', title='Horror Stories Before Bed', genre='Horror', year=2017, price=18.0, instock=0)]
Library(Harry Potter, Hunger Games, The Quest, Pokemon, Maze Runner, Horror Stories Before Bed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM