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
2
answers
546
views
How to store a collection of constants that can be called by Collection.variable?
I'm searching for a collection that can be used to store multiple constants and also can be used to get a list of them?
EDIT: It does not have to be constants, it can be variables.
I want to get a ...
0
votes
1
answer
396
views
Inheriting from Generic and NamedTuple fails with "missing required positional argument"
I've encountered a problem when trying to define a generic (typing/mypy Generic) NamedTuple. I've managed to reduce it to smallest possible working example:
a.py:
from typing import NamedTuple
from ...
-1
votes
1
answer
343
views
Python random.choices index with named tuples
So I want to know the position of the randomly named_tuples sampled by the random.choices.
I sample it with a list of the same length and positions (memory, probabilities)
sample_indices = random....
7
votes
1
answer
1k
views
Julia convert NamedTuple to Dict
I would like to convert a NamedTuple to a Dict in Julia. Say I have the following NamedTuple:
julia> namedTuple = (a=1, b=2, c=3)
(a = 1, b = 2, c = 3)
I want the following:
julia> Dict(zip(...
7
votes
2
answers
1k
views
Why are NamedTuples and (immutable) structs separate?
Can someone explain why NamedTuples and immutable structs are separate instead of NamedTuples being an anonymous struct like there are anonymous functions function (x) x^2 end? They look like they ...
0
votes
1
answer
61
views
Unable to compare types of identical but redeclared namedtuples in Python
While working on a difference engine to identify differences in very large data structures, I noticed that a type comparison between identical-but-redeclared namedtuples misbehaves. Redeclaring the ...
0
votes
1
answer
39
views
Replace a element in namedtupled of a nested list
I would like to edit the vote, but I cannot find how to do it. Please help
this_list = [Mascot(mascot_name='Peter', species='Anteater', school_name='UC Irvine', number_of_votes=137988),
Mascot(...
1
vote
1
answer
406
views
Converting Python tuple to C#
I am in the process of converting a program from Python to C#. I'm about 80% there, but I came across this code in Python and I am not sure exactly how this would translate over to C#:
from typing ...
1
vote
0
answers
39
views
4 Seasons - not producing output in pyCharm, but shows Invalid in other IDE
I am writing a program that takes input from the user for a Month and Day, and then determines which of the four seasons the day belongs to. We were given the specific seasons below, and the user is ...
-2
votes
2
answers
218
views
Getting Syntax Error in Keyword Class Patterns
City class and a few instances
import typing
class City(typing.NamedTuple):
continent: str
name: str
country: str
cities = [
City('Asia', 'Tokyo', 'JP'),
City('Asia', 'Delhi', '...
1
vote
0
answers
140
views
Creating tuples of images and distributing the tuples into training and testing sets
I've a simple program that creates a bunch of images. The problem is, they need to be ordered into tuples of three images so that the images in each tuple are kept together when they are distributed ...
9
votes
3
answers
1k
views
typing.NamedTuple and mutable default arguments
Given I want to properly using type annotations for named tuples from the typing module:
from typing import NamedTuple, List
class Foo(NamedTuple):
my_list: List[int] = []
foo1 = Foo()
foo1....
0
votes
1
answer
76
views
Convert txt to JSON with Python using namedtuple
I'm trying to convert a txt file to JSON using this:
import json
import collections
def main():
file = open("file.txt")
#structure the output using namedtuple
UserBase = ...
1
vote
1
answer
445
views
How to get key and values using NamedTuple
Python 3.9.6
I have been trying to figure out on how I am able to print the key and value when I have given variable that I want to specific get from a NamedTuple.
I have created my own NamedTuple ...
0
votes
1
answer
211
views
How to construct circular referencing instances of a frozen class in python
I have instances of a dataclass that reference each other.
from dataclasses import dataclass
@dataclass()
class Foo:
id: int
neighbor: 'Foo'
foo = Foo(1, None)
bar = Foo(2, foo)
foo....