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
550
questions
1
vote
0
answers
36
views
Setting the return type of a custom namedtuple
I have a sample_config.json file with potentially an arbitrary number of the parameters. An example of the sample_config.json is as follows:
{
"tableA_ID": "tableA.csv",
"...
1
vote
1
answer
35
views
Why pyomo unpack namedtuples in constraints?
I am new to pyomo and I want to understand why it works with namedtuple the way it works.
There is a code sample:
from collections import namedtuple
import pyomo.environ as pyo
Product = namedtuple(&...
0
votes
1
answer
20
views
Dict comprehension over multiple dicts with missing keys
I have a piece of legacy python2 code that looks like the following
# dict1 and dict2 are inputs. dict2 is where the value is a 4-tuple
return {
key: [
NamedTuple1(val1, ...
1
vote
1
answer
37
views
How can I access the name of namedtuple attribute from inside a method if it wasn't passed as a string?
This is a followup to How can I pass a namedtuple attribute to a method without using a string?.
Given this solution to the above:
from typing import NamedTuple
class Record(NamedTuple):
id: int
...
0
votes
1
answer
71
views
Trying to understand behavior of `_field_defaults` of `namedtuple` in Python 3.9 [closed]
I am new to Python's namedtuple, particularly, _field_defaults.
According to Python 3.12.3 official doc,
some namedtuple._field_defaults
Dictionary mapping field names to default values.
Would ...
-1
votes
1
answer
55
views
Get the total count of 1 field of a list of namedtuple objects
In Python, what is the easiest way to get the total count of one field from a set of named tuples?
In this example, I'm looking for the total count of TestResults.failed, which should be 4. It's ...
0
votes
2
answers
52
views
Conversion of Dataframe to namedtuple of numpy arrays
I need to convert my pandas dataframe to tuple of numpy arrays. My code is like,
import pandas as pd
data = {'Class':[10., 11., 9., 8.],
'Age':[27., 24., 22., 32.],
'Mark':[76., 56., ...
0
votes
1
answer
126
views
_pickle.PicklingError: Can't pickle <class '__main___.{CONST} '>: attribute lookupfaileddir_namesmain_on __main__ failed
firstly dir_names works well when pickling
dir_names = collections.namedtuple('dir_names', ['mark', 'category'])
pickle.dump(dir_names, open('tmp.bin', 'wb'))
I want make dir_name a const
DIR_NAMES = ...
0
votes
1
answer
54
views
define the args of a function outside the actual call
I have a function that has to call other functions, with a not-fixed number and type of arguments, that I want to set in a dictionary.
Something like this:
def main_function(...
0
votes
1
answer
78
views
How is Python able to distinct type names of namedtuples? [duplicate]
My model class has to provide lots of queries and I would like to return query results as namedtuple. My solution is working, but I don't know why :) - so I would like to get some clarification ...
1
vote
0
answers
27
views
Is it Possible to extract the name of an Argument in TypeScript? [duplicate]
Using Parameters I can get the argument types, even as named tuple, but how can I access the name of the argument (or the tuple for that matter), is it possible in the latest TypeScript Version (5.1.6)...
0
votes
1
answer
83
views
Why does namedtuple + namedtuple return a regular tuple?
from collections import namedtuple
foo_a = namedtuple("foo_a", ["a1", "a2", "a3"])
foo_b = namedtuple("foo_b", ["b1", "b2", "...
2
votes
1
answer
350
views
Using a cached property on a named tuple
from typing import NamedTuple
from functools import cached_property
class Rectangle(NamedTuple):
x: int
y: int
@cached_property
def area(self):
return self.x * self.y
I ...
0
votes
1
answer
129
views
Print named tuple value in a list
A function is returning me the below list of named tuples:
[popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=...
1
vote
1
answer
33
views
Is it be possible to give custom names to the newly created type that is pushed to the end of a tuple type, without affecting other argument names?
Here i have a type that takes a tuple type with a fixed number of parameters, and also includes an array of strings as the final parameter. I want to give the tuple type parameters custom names a, b ...