Skip to content

Commit eb69bdb

Browse files
committed
Namedtuple
1 parent 52a6a5d commit eb69bdb

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

namedtuple_ex.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import namedtuple
2+
3+
Book = namedtuple("Book", "author title genre")
4+
books = [
5+
Book("Pratchett", "Nightwatch", "fantasy"),
6+
Book("Pratchett", "Thief Of Time", "fantasy"),
7+
Book("Le Guin", "The Dispossessed", "scifi"),
8+
Book("Le Guin", "A Wizard Of Earthsea", "fantasy"),
9+
Book("Turner", "The Thief", "fantasy"),
10+
Book("Phillips", "Preston Diamond", "western"),
11+
Book("Phillips", "Twice Upon A Time", "scifi"),
12+
]
13+
14+
fantasy_authors = { b.author for b in books if b.genre == 'fantasy'}
15+
# Il s'agit d'un set {} pour ne pas retourner les doublons
16+
print(fantasy_authors)
17+
18+
# a = {1,2,2}
19+
# a
20+
# {1, 2}

0 commit comments

Comments
 (0)