I am practicing on a piece of code from the O'Reily's book Fluent Python.
The piece of code is as followed:
import collections
Card= collections.namedtuple('Card',['rank','suit']);
class FrenchDeck:
ranks=[str(n) for n in range (2,11)]+ list('JQKA')
suits= 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards= [Card(rank,suit) for rank in self.ranks
for suit in self.suits]
def __len__(self):
return len(self._cards)
def __getitem__(self,position):
return self._cards[position]
I can see how the feature of namedtuple is used to assign each value in ranks to all suits.
As one of my own practices, I would like to ask for help about assigning values in this paradigm. Specifically, in this snippet, instead of giving one students four grades, what's the way to assign one students with their respective unique grade using the namedtuple feature? (e.g John-89, Jay-98, Joe-87, Joseph-91). The way I regurgitate from the sample in the book will still assign all grades to each students. Thanks a lot for your attention and help!
Student= collections.namedtuple('Student',['name','grade'])
studentname=["John","Jay","Joe","Joseph"]
class grade:
names= studentname
grades=[89,98,87,91]
def __init__(self):
self._stud=[
Student(name,grade) for name in self.names
for grade in self.grades ]
def __len__(self):
return len(self._stud)
def __getitem__(self,position):
return self._stud[position]