编写一系列语句以打印命名元组中的特定类别
Writing a sequence of statements to print a certain category in a namedtuple
编写一系列语句,打印 BSI 中每本书的标题,每行一个。
from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
BSI = [
Book('Tyler Hawkin','ABC','education', 2009, 20.00, 75),
Book('Miss Hanigan','Jannie','adventure', 1900, 26.00, 51),
Book('Leila Star','My first crush','comedy', 2013, 8.89, 11),
Book('John Green', 'Fault in our Stars', 'romance' ,2006, 17.00, 0),
Book('Shakespeare', 'Romeo', 'Drama', 1610, 5.00, 99),
Book('Janett Smith','How to be Young Again','life', 1995, 13.00, 3) ]
def book_title(book:"Book") -> str:
return book.title
使用上面的代码,我可以调用任何书名,但我该如何更改此代码,以便它每行打印所有书名?
由于元组列表是可迭代的,您可以遍历命名的元组
for book in BSI:
print(book.title)
ABC
Jannie
My first crush
Fault in our Stars
Romeo
How to be Young Again
编写一系列语句,打印 BSI 中每本书的标题,每行一个。
from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
BSI = [
Book('Tyler Hawkin','ABC','education', 2009, 20.00, 75),
Book('Miss Hanigan','Jannie','adventure', 1900, 26.00, 51),
Book('Leila Star','My first crush','comedy', 2013, 8.89, 11),
Book('John Green', 'Fault in our Stars', 'romance' ,2006, 17.00, 0),
Book('Shakespeare', 'Romeo', 'Drama', 1610, 5.00, 99),
Book('Janett Smith','How to be Young Again','life', 1995, 13.00, 3) ]
def book_title(book:"Book") -> str:
return book.title
使用上面的代码,我可以调用任何书名,但我该如何更改此代码,以便它每行打印所有书名?
由于元组列表是可迭代的,您可以遍历命名的元组
for book in BSI:
print(book.title)
ABC
Jannie
My first crush
Fault in our Stars
Romeo
How to be Young Again