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
0
votes
1
answer
62
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 ...
0
votes
1
answer
37
views
How can I make a data structure containing a variable so that it updates when the variable is re-defined?
I am making a text adventure game. I'm using a named tuple to define locations.
Location = namedtuple('Location', ['desc', 'ldesc', 'func', 'dirloc'])
entrance = foyer = None
entrance = Location('...
2
votes
1
answer
329
views
How to alias class attributes when creating NamedTuple?
I want to alias project_version with init_version, but since NamedTuple is a factory method I'm having difficulty in doing this.
from typing import NamedTuple
class ProjectMetadata(NamedTuple):
&...
1
vote
1
answer
506
views
PyCharm: namedtuple: unexpected argument, unfilled parameter
In PyCharm, you can declare a named tuple.
from collections import namedtuple
InstTyp = namedtuple(
typename='InstTyp',
field_names='''
instance_type
memory
num_cpus ...
-1
votes
1
answer
34
views
How to type a returned Array inside an Array? [closed]
return [[x, y], d, prevRing, prevRingPosition]
That is my return statement and I want to make a type for its function
type nextUlamReturn = [
number[],
d: number,
prevRing: number,
...
1
vote
1
answer
225
views
NamedTuple - сhecking types of fields at runtime
Is there a neat solution to raise an error if a value is passed to the NamedTuple field that does not match the declared type?
In this example, I intentionally passed page_count str instead of int. ...
3
votes
1
answer
415
views
Why does mypy fail to detect that a raw tuple value is a NamedTuple?
I have this piece of code:
from typing import NamedTuple
class KeyTuple(NamedTuple):
key: str
storage: dict[KeyTuple, int] = {}
storage.get(("kek",))
that raises an error:
test_mypy....
1
vote
1
answer
131
views
Difference between a class(namedtuple) and a namedtuple
What is the difference between class(namedtuple) and only namedtuple, they look different but in both cases sensor seems to be the same thing, and what kind of functionality can be added inside class ...
-1
votes
1
answer
462
views
How to find delete a student record in Python (example on a Students Mark exercise)
Start a new Python script and write some suitable input commands to store three items of data into
three variables: Student Name, Coursework Mark, and Exam Mark. The inputs for coursework mark
and ...
2
votes
0
answers
46
views
Any dict-like mappable objects in Python that only show preview of contents when __repr__?
I'm looking for an object I can use to store largish dataframes and sklearn objects. Ideally I would like to store them as pd.Series because it has the behavior I'm looking for in that I can do the ...
0
votes
2
answers
99
views
NamedTuple is shared across variables
from typing import NamedTuple, List, Set, Tuple, Dict
class EmbeddingInfoStruct(NamedTuple):
emb_names : list[str] =[]
idx_in_data: list[int] =[]
emb_dim: list[int] =[]
info1 =...
0
votes
1
answer
48
views
Python data json dictionary
I wanted to read tar.json file, so I write:
import json
with open('tar.json', "r", encoding='utf-8') as read_file:
data = json.load(read_file)
and print it as dictionary, for ...
0
votes
1
answer
2k
views
How to access name of named tuple in Typescript
I want to access the name from a named tuple to use within one of my functions. Is there a way to access the name only, similar to how you can access the type only by using the index of the tuple ...
0
votes
0
answers
36
views
Dynamically extend `__del__` cleanup code on an instance
I want to bind a dynamically created namedtuple to a class
When an instance of the class is initialized, a new namedtuple-type is created, and registered in globals() (this allows pickling).
When ...
0
votes
1
answer
286
views
Use Tuple values in Object initalizer syntax
I'm trying to initialize properties of an object I am creating with the values of a named tuple.
Something like this
public Person DoIt() {
return new Person {
(First, Last) = GetFirstAndLast(id)...