from collections import namedtuple
Book = namedtuple('Book', 'title author year price')
best = Book('John Dixie',
'James Pi', 1922, 22.60)
better = Book('Clifford',
'Jane Doe', 2005, 20.00)
worst = Book('Nightingale',
'Daniel Smith', 1905, 27.90)
booklist = [best, better, worst]
With this given information, how do I create a boolean expression that the price of booklist[0]
is higher than that of booklist[1]
? I tried comparing booklist[0[3]]
to booklist[1[3]]
, but I don't think that's the correct format for comparing a namedtuple within a list. How should I do this?
If you have the book (eg booklist[0]
), just write [3]
after it (no spaces). But prefer to use the name, not an index. It's a named tuple.
Whether you have the book in a variable or accessed in a different way (in this case by getting an element from a list) is irrelevant.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.