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
2 answers

Python: Json to object, ignore certain characters

Currently I am converting a json to a Python object using the namedtuple class. This way I can easily select data for e.g. print(json.some_json_variable). However, my Json contains the following key: "@odata.context" and this ofcourse, cannot be…
Marc Dirven
  • 309
  • 2
  • 18
0
votes
2 answers

Using static NamedTuples variables outside of class in a function

I'm using a NamedTuple to define a server to which I want to connect using telnetlib. Then I created a class which defines the connection to the server, with the server details and connection method inside the class. Then outside of the class, I…
Alon
  • 99
  • 1
  • 9
0
votes
0 answers

Converting List of words to Problem Graph For Minimum Spanning Tree Algorithm

I have been trying to construct a problem weighted Graph for Minimum Spanning Tree from a list or string of words as, given a string of words to list of named-tuple with every possible combinations of words as head and tail with additional head…
0
votes
0 answers

How can you name a tuple used as a function parameter in C#?

I want to use a tuple as an input argument for my function. public void Sync(IEnumerable> labels) But I also want to give the string and double a name. But I can't get the syntax right. I am looking at all kinds of other blogs…
0
votes
1 answer

How do I correctly assign names to a named tuple in a list

I have the following code and it seems to work fine, but when I inspect the tuples inside the list they are named Item1,Item2,Item3 instead of the names I have assigned to them. What am I doing wrong? (The code references System.ValueTuple.) Thank…
AlePorro
  • 111
  • 1
  • 11
0
votes
1 answer

Pickle doesn't work for class inherited from "namedtuple" when overriding "__new__"

The following code fails from collections import namedtuple import pickle class Foo(namedtuple("_Foo", ["a", "b"])): def __new__(cls, **kwargs): self = super().__new__(cls, **kwargs) # some custom code return self foo =…
knub
  • 3,892
  • 8
  • 38
  • 63
0
votes
1 answer

Creating namedtuple valid for differents parameters

I'm trying to figure it out a way to create a namedtuple with variable fields depending on the data you receive, in my case, I'm using the data from StatCounter and not on all the periods are the same browsers. I tried this way but it is a bit ugly…
Davichete
  • 415
  • 7
  • 14
0
votes
2 answers

namedtuple TypeError count of positional arguments

Why does the TypeError message indicate 3 positional arguments if the namedtuple only calls for two? Why does it say that 4 were given? from collections import namedtuple Transition = namedtuple('Transition', ('one', 'two')) a =…
Doug Edmunds
  • 511
  • 3
  • 14
0
votes
1 answer

dictionary | class | namedtuple from YAML

I have a large-ish YAML file (~40 lines) that I'm loading using PyYAML. This is of course parsed into a large-ish dictionary plus a couple of arrays. My question is: how to manage the data. I can of course leave it in the output dictionary and work…
Carlo
  • 151
  • 8
0
votes
1 answer

Configuring a Tkinter Message to display tuples on individual lines

I am trying to do what the Title says, Configure the Tkinter message so that each tuple in WL_ratios has its own line without being surrounded in brackets. Creating more messages is not an option as the length of WL_ratios can vary, so i have to…
snaktastic
  • 43
  • 1
  • 1
  • 6
0
votes
3 answers

If a namedtuple is immutable, why does it have a _replace method?

Please consider this code: >>> point = namedtuple('point', ('x', 'y')) >>> p1 = point(3,4) point(x=3, y=4) >>> id(p1) 2881782633456 # address in memory >>> p1._replace(x = 78) point(x=78, y=4) >>> id(p1) 2881782633456 # same as before. Seems…
Gsbansal10
  • 303
  • 5
  • 12
0
votes
2 answers

Working with named tuples to output specific data

I am having some trouble working with initializing my data so that I can call specific values by their keys... This is my code so far: from kafka import KafkaConsumer import ast from collections import namedtuple import json import csv import…
j.Doe
  • 87
  • 6
0
votes
1 answer

Decorator to transform the return type of a function from a dictionary to a namedtuple

I wrote a hack gist to decorate a function that returns a dictionary so that the dict is transformed into a namedtuple. It has many weak points and I'd like to know from the python gurus if there are other ways for a more robust version Weak…
jmborr
  • 1,215
  • 2
  • 13
  • 23
0
votes
1 answer

How to rename a namedtuple argument in pycharm

Config = namedtuple( 'c', ['height'] ) DEFAULT_CONFIG = Config(height=4) I want to rename the argument height to start_height everywhere in my code, including mentions of config.height and calls to Config(height=4) When I click refactor on the…
Sam Shleifer
  • 1,716
  • 2
  • 18
  • 29
0
votes
2 answers

Reading from text-file with namedtuple entries

I have a problem. I had a list consisting of namedtuples. I wrote this list to a file.txt. Now i want to read it back. I found out here that json is recommended for converting it beforehand and than saving it. But I need a solution for my already…