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

Pass list of elements to named tuple

I'm suppose to create a namedtuple which has 27 field_names. Though it has too many field_names I created a list called sub which has list of items for field_names. The result is my reference to the instance of namedtuple. sub = [ 'MA9221',…
Balakrishnan
  • 2,403
  • 2
  • 26
  • 47
1
vote
1 answer

Python put Database records in Namedtuple

I'm trying to write some code in python (2.7) doing this: Open a database in sqlite Do a query to the database, getting some results. The database has more than one table and i need the records from different tables: database is like this: data.db…
Rigel
  • 143
  • 3
  • 13
1
vote
1 answer

How does Property work with Itemgetter im Python?

I was learning the python. And when comes to the collection module in official library, I found a code snipet of the NamedTuple like: for i, name in enumerate(field_names): template += " %s = _property(_itemgetter(%d), doc='Alias for…
1
vote
0 answers

Malformed String error - Python - converting string repr. of dictionary

When I do ast.literal_eval(), I get ValueError: malformed string for the line below z = ast.literal_eval(a). Why is this not working? (Pls. note - "something" in the line below has 4 or more namedtuples) for thing in something: a =…
askance
  • 1,077
  • 4
  • 14
  • 19
1
vote
0 answers

Optimizing modifiable named list based on namedtuple

My goal is to optimize a framework based on a stack of modifiers for CSV-sourced lists. Each modifier uses a header list to work on a named basis. CSV example (including header): date;place 13/02/2013;New York 15/04/2012;Buenos…
Benoît
  • 16,798
  • 8
  • 46
  • 66
1
vote
1 answer

Is there a better way to do csv/namedtuple with urlopen?

Using the namedtuple documentation example as my template in Python 3.3, I have the following code to download a csv and turn it into a series of namedtuple subclass instances: from collections import namedtuple from csv import reader from…
MikeRand
  • 4,788
  • 9
  • 41
  • 70
1
vote
1 answer

python ctypes vs namedtuple

So I have two simple ctypes struct class S2 (ctypes.Structure): _fields_ = [ ('A2', ctypes.c_uint16*10), ('B2', ctypes.c_uint32*10), ('C2', ctypes.c_uint32*10) ] class S1 (ctypes.Structure): _fields_ = [ ('A', …
Juster
  • 385
  • 2
  • 3
  • 11
1
vote
2 answers

Passing 'argument lists' as a namedtuple in python

I created a namedtuple called Engine that holds most of the information I want to pass around my program. Inside of this tuple, are a couple of parameters and two instanced classes that are used by methods downstream. Engine =…
kreativitea
  • 1,741
  • 12
  • 14
0
votes
1 answer

Why does namedtuple + namedtuple return a regular tuple?

from collections import namedtuple foo_a = namedtuple("foo_a", ["a1", "a2", "a3"]) foo_b = namedtuple("foo_b", ["b1", "b2", "b3"]) a = foo_a(1, 2, 3) b = foo_b(4, 5, 6) a + b # -> (1, 2, 3, 4, 5, 6) type(a + b) # -> tuple It seems that…
sbwcwso
  • 15
  • 5
0
votes
1 answer

Using a cached property on a named tuple

from typing import NamedTuple from functools import cached_property class Rectangle(NamedTuple): x: int y: int @cached_property def area(self): return self.x * self.y I thought this class definition would complain…
0
votes
1 answer

Print named tuple value in a list

A function is returning me the below list of named tuples: [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)] I want to print the…
0
votes
1 answer

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 code: data = [] req = namedtuple('Req', 'a b c d e…
user34088
  • 21
  • 4
0
votes
1 answer

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('Dungeon Entrance', ( 'You are in a small clearing…
Odoul
  • 23
  • 4
0
votes
0 answers

How can I mock the output of a named tuple?

I have a piece of code which outputs a list of named tuples - something like this: for entry in info: my_tuple = namedtuple('some_info', ['name', 'age']) list_of_tuples.append(some_info(info.name, info.age)) where info contains the name and…
JJH
  • 1
  • 2
0
votes
0 answers

Converting Namedtuples Returned From Modules to Dictionaries in Python

When working with a namedtuple value returned by an imported module, ._asdict() is not found to be a valid attribute. My attempted usage : >>> import os >>> os.get_terminal_size() os.terminal_size(columns=120, lines=30) >>>…