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

Values recovery from named tuples using list comprehension inside function with property decorator?

I'm following the python documentation and this SO answer also this but i'm getting a property object pointer when using getattr and im unable to get or print the value i'm looking for. class DefaultSettings(): """This contains default game…
Strapicarus
  • 131
  • 1
  • 5
-1
votes
1 answer

nameTuple and class parameters

My lecturer wrote some code and I have no idea how to read it and google isn't helping, can someone please clarify how this code interacts. yield Arc(tail_node, head = tail_node - 1, label="1down", cost=1) Is the method call for the class class…
-1
votes
1 answer

namedtuple: expected 1 argument, got 3

I have this basic namedtuple: from collections import namedtuple DBInfos = namedtuple('dbname', 'dbpath', 'dbfile') When I try to instantiate one with d = DBInfos._make(['test', 'a/b/c', '.index.db']) I get the following error: Traceback (most…
valentin
  • 2,596
  • 6
  • 28
  • 48
-1
votes
1 answer

trouble with select title from a namedtuple list

I want to select the title of the book and make a new list in alphabetical order of that Book = namedtuple('Book', 'author title genre year price instock') BSI = [Book('JK Rowling', 'Harry Potter', 'Fiction', 2009, 12.50,…
Lu Jiang
  • 1
  • 1
-1
votes
2 answers

Python Namedtuple Indexing Instance

So Here is my code for my lab coding project that I am currently working on: from collections import namedtuple Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price') # Restaurant attributes: name, kind of food served, phone number,…
-2
votes
1 answer

Write a new CSV File from Existing CSV File with many Conditions

I want to write a CSV File which gets Data from and depending from an existing CSV File. Existing_FILE: Date, Value, Name, sh 2022-01-15, 30,00, Monthly1 - #1500, H 2022-01-18, 130,00, Monthly50, S 2022-01-24, 344,00, Bill # 1110 .. blabla,…
-2
votes
2 answers

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', 'IN'), City('North America', 'Mexico City',…
Faisal Nazik
  • 2,367
  • 4
  • 17
  • 40
-2
votes
1 answer

Copying all despite of one attributes/fields of one to another namedtuple

What's the shortest way to copy all despite of one attribute/field from one to another namedtuple? It's possible to do it like follows. initial_person = Person(name='Bob', age=30, gender='male') new_age = 31 modified_person =…
thinwybk
  • 4,193
  • 2
  • 40
  • 76
-2
votes
1 answer

Dynamically create classes- Python

I'm trying to figure out what would be the best way to create classes in a dynamic manner based on the contents of a JSON file. So for example, here's a snippet from the JSON file: { "stuff": [{ "name": "burger", "aka":…
David West
  • 552
  • 2
  • 9
  • 21
-2
votes
1 answer

How do I access the individual indexes of a list of named tuples within a defaultdict?

I have created a defaultdict using a csv file. The defaultdict has a movie director as the key and a list of named tuples as the values. The named tuples have 3 elements: title, year and score. I need to isolate the year element, check whether the…
balter
  • 25
  • 1
  • 9
-2
votes
2 answers

How to loop until all lines read from a file?

I'm trying to set up a score system where I need to input all the scores from a text file into an 'array of records'. I'm fairly new to Python and hope for a simple solution. In my program, the array of records would technically class as a list of…
-2
votes
1 answer

How to iterate on a namedtuple object?

This is what I have: Site = namedtuple('Site', 'number x y') Sites = namedtuple('Sites', 'p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14') my_sites = Sites(Site('p1', 607, 184), Site('p2', 698, 254), Site('p3',…
TrustMe
  • 9
  • 1
  • 5
-2
votes
2 answers

How to cut my addiction to Python dictionaries

So, I have a large 30k line program I've been writing for a year. It basically gathers non-normalized and non-standardized data from multiple sources and matches everything up after standardizing the sources. I've written most everything with…
gunslingor
  • 1,358
  • 12
  • 34
-2
votes
1 answer

How to create Boolean expression from a namedtuple within a list on Python

from collections import namedtuple Book = namedtuple('Book', 'title author year price') best = Book('John Dixie', 'James Pi', 1922, 22.60) better = Book('Clifford', 'Jane Doe', 2005, 20.00) worst =…
Deer530
  • 73
  • 1
  • 1
  • 10
-2
votes
1 answer

How to create a list of named tuples from a very long JSON nested list?

The JSON-file looks like this [["bla", "bla",] ["bla2", "bla2"] [..] [..]] The JSON-file consists of hundreds of these list. I need to get the ["bla", "bla"] part out of the list and make it an named tuple. how can I do this?
1 2 3
36
37