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

How to set value of class object argument as if its a dict using __setItem__

I have implemented a sample class PointLocation, import collections as _collections Point = _collections.namedtuple("Point", ("x", "y", "z")) class PointLocation(object): def __init__(self, x, y, z): self._x = x self._y = y …
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
0
votes
2 answers

choosing right data structure to parse a file

I have a csv file with contents in the following format: CSE110, Mon, 1:00 PM, Fri, 1:00 PM CSE114, Mon, 8:00 AM, Wed, 8:00 AM, Fri, 8:00 AM which is basically course name followed by it's timings. what's the best data structure to parse and store…
raghu
  • 131
  • 3
  • 13
0
votes
1 answer

How to implement a tree structure in Python using namedtuple

I have a key-word, e.g. friendly. It gives birth to a child-word, e.g. warm, while descending from a parent-word, e.g. friend. from collections import namedtuple keyword = 'friendly' childword = 'warm' parentword =…
wen
  • 1,875
  • 4
  • 26
  • 43
0
votes
1 answer

Alphabetical sort in python

Write a sequence of statements that prints the title of each book in BSI, one per line, in alphabetical order; do this without changing the original order of BSI. My code: from collections import namedtuple Book = namedtuple('Book', 'author title…
anonymous fox
  • 39
  • 1
  • 5
0
votes
3 answers

Namedtuple that store point

I want make a variable that store some points in one ROI. Let say it is something like: ROI.Point1.x ROI.Point1.y ROI.Point2.x ROI.Point2.y ... ROI.PointN.x ROI.PointN.y I tried to make this with namedtuple, like so: Point = namedtuple("Point", "x…
mas_bejo
  • 597
  • 2
  • 6
  • 22
0
votes
2 answers

counting occurrence of name in list of namedtuple (the name is in a nested tuple)

As the title says, i'm trying to count the occurrence of a name in a list of namedtuples, with the name i'm looking for in a nested tuple. It is an assignment for school, and a big part of the code is given. The structure of the list is as…
Rob
  • 13
  • 1
  • 5
0
votes
1 answer

Return tuple with value in list

I have this list of errors with their associated codes and descriptions: Exception = namedtuple("Exception", "code name description") exceptions = [ Exception(1, "ILLEGAL FUNCTION", "Definition 1"), Exception(2, "ILLEGAL DATA ADDRESS",…
Juicy
  • 11,840
  • 35
  • 123
  • 212
0
votes
1 answer

How can I make my namedtuple actually immutable?

I created a SETTINGS object as a namedtuple thinking that the list contained inside would be immutable. I was incorrect, as through the whole chain only the reference to the original list inside an entirely-mutable dict is used. Thus, when I call…
Bryson
  • 1,186
  • 2
  • 13
  • 26
0
votes
1 answer

How do you store the request.form to db through wtforms or error in sqlalchemy update?

This is following on from this question: SQLalchemy/wtforms update issue - 400 bad request I have a flask framework Issue When I submit the form the flash message comes up saying prediction added although when I query the db nothing has changed??…
Lorbat
  • 385
  • 2
  • 8
  • 27
0
votes
2 answers

Is a namedtuple the way to go for a flexible container?

I am quite new to Python and I don't know what is available. Currently, I have this piece of code to quickly put some named variables together, so I can use them in other places: def myfunction(): props = namedtuple('props', '') props.color…
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0
votes
2 answers

python tuple immutable but set type attribute can be changed

I understand that a namedtuple in python is immutable and the values of its attributes cant be reassigned directly N = namedtuple("N",['ind','set','v']) def solve() items=[] R = set(range(0,8)) for i in range(0,8): …
Pratyush Dhanuka
  • 1,405
  • 2
  • 11
  • 21
0
votes
4 answers

Best option to store data for printing in certain format

So I have some csv data all I need from this data is two fields then I will do some calculations using the close price and have two more fields and print those four fields in a certain format. I was thinking of creating namedtuples with…
user2423347
0
votes
3 answers

Which data structure to use as an array of dicts?

I need to build a data structure like this one: { key: {k: v for k in range(fixed_small_number)} for key in range(fixed_large_number) } The thing is I'm building it in an "eclectic" way, where every time get one more item to put in a random…
phistakis
  • 211
  • 1
  • 9
0
votes
1 answer

Python Different the datetime in a Struct Array

import struct from collections import namedtuple StructDeviceInfo = namedtuple('DeviceInfo', ['DeviceID', 'Capturing','Receiving','Socket','DateTime']) DeviceInfoList = [] def threaded_function(): while True: if any(x.Capturing == True…
user2040602
0
votes
1 answer

python print only the names from text file

I can't get it to print only the names. This is what I got so far and here is the link for the malenames.txt: http://www.ics.uci.edu/~kay/malenames.txt from collections import namedtuple FN = namedtuple('FN','name percent people rank') FirstN =…