python - Represent a table as object -
is there standard way represent table contains relational data in python ? mean, this:
singular plural 1st. make make 2nd. make make 3d. makes make
i data accessible both rows , columns, this:
1st. singular -> make 1st. -> make, make plural 3d. -> make plural -> make, make, make
i can't see way store data efficiently, without redundancy. better can think of use multiple dictionaries (one per row , 1 per column), each contain many keys there rows or columns associated dictionary itself, plus 1 special key contain associated values.
i guess has been addressed, that's why ask.
as alternative other answer, can use namedtuple
suggested @jamylak:
from collections import namedtuple class verb(namedtuple("_verb", # arbitrary class name/tag ["singular1", "singular2", "singular3", "plural1", "plural2", "plural3"])): @property def singular(self): return (self.singular1, self.singular2, self.singular3) # plural @property def first_person(self): return (self.singular1, self.plural1) # 2nd , 3rd person
now "make" can represented as
verb("make", "make", "makes", "make", "make", "make")
and again, can optimized leveraging simplicity of english conjugations.
the downside of solution doesn't allow changing individual fields in table, because namedtuple
immutable. if want make changes, use ordinary class
__slots__
.