Questions tagged [namedtuple]

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

Documentation: collections module in Python 3.

Recently, a declarative API for namedtuples is also provided by the typing module.

545 questions
0
votes
1 answer

How to return collection from a function

I'm working with collection's namedtuple to return a list of tuple from within a function as such: def getItems(things_list) -> list: for i, j in enumerate(things_list): [*things_id] = things_list[i].id [*things_title] =…
0
votes
1 answer

How to create a tuple from dictionaries?

from collections import namedtuple BookPlan = namedtuple('Book', ['size', 'weight']) books = BookPlan(3, 5), BookPlan(4, 9) i=0 book={} while (i<3): book['size'] = i book['weight'] = i*5 i=i+1 books=books+BookPlan(**book) print…
Vad_S
  • 23
  • 3
0
votes
0 answers

What is the substitute of _source attribute for namedtuple in python 3.8.5?

I am doing something like below :- from collections import namedtuple Stock=namedtuple('Stock', ''' symbol year month day open …
Invictus
  • 4,028
  • 10
  • 50
  • 80
0
votes
1 answer

Data container with numeric field names

Is there a way in python to create a data container with numeric field names? A minimal example: I have variables called beta0, beta1, beta2 and would like to pack them into a container beta and access them by beta.0, beta.1, beta.2. I checked…
0
votes
1 answer

Python: TypeError: 'float' object is not iterable

I tried to do calculation in python using values accessed for any pair of two rows when iterating through a tuple list but I received the type error as below. Does anyone have idea how to solve this? Thanks! The code is shown below: for ws1 in…
0
votes
1 answer

Is it possible to pass a variable of type 'type' to a ComboBox so that all the elements of the collection are selectable?

I am trying to create a GUI app that lets me plot any variable against any other variable. This is my first GUI app, so perhaps I'm biting more than I can chew. The values of these variables are in arrays, which I have grouped into collections --…
jrive
  • 218
  • 2
  • 3
  • 14
0
votes
2 answers

Using NamedTuple inside NamedTuple with Optional typing

Is it possible to create NamedTuple inside NamedTuple like this? from typing import NamedTuple, List, Optional class BabyData(NamedTuple): name: str age: int body_measurement: Optional[NamedTuple('height': List, 'weight': List)] =…
eng2019
  • 953
  • 10
  • 26
0
votes
1 answer

Retrieve all named element from namedtuples in a subset of a list in Python

I have a list of namedtuples. I would like to generate a list of a specific named element of a subset of the namedtuples in the list, based on a list of indices to select. Example would be: from collections import namedtuple from operator import…
Peter N. Steinmetz
  • 1,252
  • 1
  • 15
  • 23
0
votes
1 answer

How do classes inherited from namedtuple maintain access to parent properties with a redefined __init__?

Minimum Reproducible Example: from collections import namedtuple class Test(namedtuple('Test', ['a','b'])): def __init__(self, a, b): self.c = self.a + self.b def __str__(self): return…
ajoseps
  • 1,871
  • 1
  • 16
  • 29
0
votes
1 answer

How to group data from a list of namedtuples

In python, I have the following data in a list of namedtuple in memory: from collections import namedtuple perfvalues=[] perfitem = namedtuple('perfitem', 'cluster host database diskgroup disk read_bytes_per_sec write_bytes_per_sec avg_ms_per_read…
Laurent D.
  • 27
  • 1
  • 4
0
votes
1 answer

How can I get a named tuple instance from its name in python?

I have a set of named tuple instances. E.g.: CostFN = namedtuple('CostFN', ['name', 'func', 'args']) tuple_lin_1 = CostFN(name="lin_1", args=args_for_1, func1=func_1) tuple_lin_2 = CostFN(name="lin_2", args=args_for_1, func1=func_2) …
mousomer
  • 2,632
  • 2
  • 24
  • 25
0
votes
1 answer

Convert string to property - AttributeError: can't set attribute

I was running python code below: def dict2struct(d): res = namedtuple("config", d.keys())(*d.values()) return res cfg = {'fieldx': 'Allan', "fieldy": 45, 'fieldt': {'head': False, 'number': 2}} res = dict2struct(cfg) print(res) res.fieldx =…
Ken S
  • 315
  • 1
  • 3
  • 11
0
votes
0 answers

Python : How do I create an object where the superclass is both blank and inherits from typing.NamedTuple?

I would like to set up an abstraction where I would like to only allow the sub datatypes of Foo to be passed in parts of my application. All of these really are typed NamedTuples, as they carry no functionality. This leads me to have Foo inherit…
FinnM
  • 394
  • 1
  • 3
  • 17
0
votes
2 answers

How to print contents of named tuple from PSUTIL

Trying to display psutil values thus:- for VM psutil.virtual_memory(): print (VM) This prints the values but not the names. Any suggestions?
kdth
  • 3
  • 2
0
votes
0 answers

reproducing a tree data structure (child/parents) with namedtuples in python

I am trying to nest nametuples trying to create a data structure where the data could ultimately be accesed with this nomenclature: parent.child.var1.a i.e. dot notation without brackets. i know that might be better coded in other ways but I am just…
JFerro
  • 3,203
  • 7
  • 35
  • 88