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
1 answer

Update a field within a NamedTuple (from typing)

I'm looking for a more pythonic way to update a field within a NamedTuple (from typing). I get field name and value during runtime from a textfile und therefore used exec, but I believe, there must be a better way: #!/usr/bin/env python3.6 # -*-…
JDi
  • 131
  • 1
  • 1
  • 5
0
votes
2 answers

From an input string to namedtuple python 2.7

Hello am a beginner on python and i want to know if ther's away to read a line and transform it to a namedtuple i will explain i have this 2 lines MARKS CLASS NAME ID 92 2 Calum 1 For the first line…
0
votes
1 answer

(Python) Sorting in decreasing sequence of the last element in a namedtuple

I am trying to sort a list of named tuples by their last 'element', in decreasing sequence (from largest to smallest). Here's a snippet of the list of namedtuples that I am trying to sort: >>> a =[] >>> a += [p] >>> a [Point(x=11, y=22)] >>> total =…
Stoner
  • 846
  • 1
  • 10
  • 30
0
votes
1 answer

Is it possible to update a NamedTuple in a single line?

class Name(NamedTuple): first_name: str last_name: str name: Name = Name(first_name="Guido", last_name="Rossum") updated = {**name._asdict()} updated.update({"last_name": "Fox"}) updated_name: Name = Name(**updated) I am aware the…
creampiedonut
  • 327
  • 1
  • 5
  • 17
0
votes
1 answer

Python namedtuple : How can I reuse variables used to create attributes in a namedtuple to get values from the namedtuple?

How can I use colname_1 to get a value for an attribute foo of a namedtuple? from collections import namedtuple colname_1 = 'foo' colname_2 = 'bar' Farm = namedtuple('Farm', 'foo bar') farm = Farm('apple', 'banana') farm.foo # OK farm.colname_1 …
Ryan Kang
  • 61
  • 1
  • 5
0
votes
2 answers

Not able to assign a value to a variable

I've been playing around with namedtuples from collections in classes, and came across this simpler 'syntactic sugar' naming assignment, but when I assign foo to 009 and try to put it into the tuple it doesn't work. Am I simple, or can someone…
0
votes
0 answers

Python: Immutable, typed, extendable class with overridable constructor?

In Python, I often find myself needing an something like NamedTuple (immutable, typed), but with an overrideable constructor and extendability. The (Python 3.6+) Syntax class MyThing(NamedTuple): thing1: int thing2: float Is nice, but…
Peter
  • 12,274
  • 9
  • 71
  • 86
0
votes
1 answer

is it possible to get a new instance for namedtuple pushed into a dictionary before values are known?

It looks like things are going wrong on line 9 for me. Here I wish to push a new copy of the TagsTable into a dictionary. I'm aware that once a namedtuple field is recorded, it can not be changed. However, results baffle me as it looks like the…
RichWalt
  • 502
  • 1
  • 4
  • 13
0
votes
1 answer

Write all data from once CSV file to another -- but include new parsed geocoding data as additional fields

I'm trying to write a Python script that will take any CSV file, run it through a geocoder, and then write the resulting geocoding attributes (+ all the data from the original file) to a new csv file. My code so far is the following, and I should…
Matt BS
  • 35
  • 8
0
votes
2 answers

Python: specific value in a named tuple of a list of tuples

I was wondering if it is possible to print a value that is inside a list of lists of tuples if you already know the indexes path. 1. List[i][j] 2. list2[x][y] 3. list3[z][w] 4. etc. I would like to do something like this: str( …
Falk
  • 11
  • 2
0
votes
1 answer

Write a function that checks if a given binary search tree contains a given value using python

I have tried almost everthing i pass the first test case but not the last two which are correctness and performance on large trees wrong and it has to return True or False. import collections class BinarySearchTree: Node =…
SaTown
  • 25
  • 1
  • 3
0
votes
1 answer

Why does are namedtuple attributes shadowed by class attributes?

With a boring class, object instance attribute shadow class attributes: class C(object): a="class_a" def __init__(self, a): self.a = a c = C(a="obja") print c.a # obja But if my class attributes are declared in a named_tuple…
user48956
  • 14,850
  • 19
  • 93
  • 154
0
votes
1 answer

Nice way to transfer to namedtuple

I'm writing a function, that translates tuple or a pair of arguments to a namedtuple with int fields. from collections import namedtuple EPS = 0.00000001 def point(*cont: 'tuple or pair of args') -> namedtuple('Point', 'x y'): """Make an int…
ANDREYDEN
  • 96
  • 10
0
votes
0 answers

Is there a way to use Pattern Matching in switch statement with Named Tuples?

Let's have the below class public void HandleMessage(object message) { switch (message) { case string bookTitle: Console.WriteLine($"Received book with title : {bookTitle}"); break; …
Amr Badawy
  • 7,453
  • 12
  • 49
  • 84
0
votes
2 answers

Using Lambda as a Key function in SORT

I have a dictionary that has company names as keys. The values are a namedtuple of two things. entity having a link to company url and grade having a numerical value. I would like to sort this dictionary based on value and specifically by grade. I…
Bharath Bharath
  • 45
  • 1
  • 1
  • 10