May-23-2019, 05:34 AM
I don't get it. One can't have different size of namedtuple object, there will be TypeError:
>>> Person = nametuple('Person', 'name gender age') >>> Bob = Person('Bob', 'male', 22) >>> Bob Person(name='Bob', gender='male', age=22) >>> Sam = Person('Sam', 'male', 20, 'student') TypeError: __new__() takes 4 positional arguments but 5 were givenTo access values in list of namedtuples:
>>> Person = nametuple('Person', 'name gender age') >>> data = [['Bob', 'male', 22], ['Alice', 'female', 21]] >>> persons = [Person(*row) for row in data) >>> persons [Person(name='Bob', gender='male', age=22), Person(name='Alice', gender='female', age=21)] >>> [person.name for person in persons if person.gender == 'male'] ['Bob'] >>> [person.name for person in persons if person.gender == 'male'][0] 'Bob' >>> persons[0].name 'Bob'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.