Skip to main content

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
1 vote
1 answer
33 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(&...
Alexey's user avatar
  • 99
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, ...
roulette01's user avatar
  • 2,346
1 vote
1 answer
36 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 ...
MikeP's user avatar
  • 323
0 votes
1 answer
69 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 ...
Ku Zijie's user avatar
  • 473
-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 ...
David Gard's user avatar
  • 11.7k
0 votes
2 answers
51 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., ...
Aristocrat's user avatar
0 votes
1 answer
123 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 = ...
king Carrey's user avatar
0 votes
1 answer
53 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(...
invalid syntax's user avatar
0 votes
1 answer
77 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 ...
Markus's user avatar
  • 11
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)...
dsalex1's user avatar
  • 454
0 votes
1 answer
80 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", "...
sbwcwso's user avatar
  • 15
2 votes
1 answer
341 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 ...
COVFEFE-19's user avatar
0 votes
1 answer
124 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=...
Saga Harby's user avatar
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 ...
cyrus-d's user avatar
  • 805
0 votes
1 answer
61 views

modifiy one element of namedtuple of list

I have written script to extract some information from pdf file. Each page is read as blocks. if [V2G has been found, then it will saved it as well as the title ,subtitle and the bulleted list. My ...
user34088's user avatar

15 30 50 per page
1
2 3 4 5
37