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

How to type a returned Array inside an Array?

return [[x, y], d, prevRing, prevRingPosition] That is my return statement and I want to make a type for its function type nextUlamReturn = [ number[], d: number, prevRing: number, prevRingPosition: number ] But this gives me the…
-1
votes
1 answer

How to find delete a student record in Python (example on a Students Mark exercise)

Start a new Python script and write some suitable input commands to store three items of data into three variables: Student Name, Coursework Mark, and Exam Mark. The inputs for coursework mark and exam mark should be validated as a value in the…
tab
  • 25
  • 4
-1
votes
1 answer

NamedTuple as String data types most versatile

from collections import namedtuple Data=namedtuple('Data','name number age cgpa')#best to keep all data types as string def prettyprint(X,end="\n"): s="{0},{1},{2},{3}".format(X.name,X.number,X.age,X.cgpa) …
Soham Patil
  • 111
  • 3
-1
votes
1 answer

Python random.choices index with named tuples

So I want to know the position of the randomly named_tuples sampled by the random.choices. I sample it with a list of the same length and positions (memory, probabilities) sample_indices = random.choices(self.memory, k=batch_size,…
-1
votes
1 answer

Trouble casting string to NamedTuple

I am trying to parse a string rep of a NamedTuple for connection parameters generated remotely by the psutil library (process.connections()) The string looks like this: pconn(fd=-1, family=, type=
Bill
  • 618
  • 6
  • 15
-1
votes
1 answer

"ValueError: not enough values to unpack (expected 12, got 1)" while creating a list of named tuples

I'm trying to create a list of named tuples from a CSV file. The error pulls up on line 12(the one starting with movie_list). It says that there are not enough values to unpack. I've done multiple other projects using this exact method, and no…
-1
votes
2 answers

AttributeError: 'tuple' object has no attribute 'get' in Python 3

I have two lists. one of candidates and one of votes received. I want to sort them descending by votes received. Zipping works fine. When I print the type of the resultant list it comes back as class 'list'. Next I sort and, bam!, get an…
Wolfe
  • 77
  • 2
  • 9
-1
votes
2 answers

Python how should I fix this if I call my function, it returns function object

I tried to call my function here and it returns function get_song_input at 0x7ff9869ee050 whats wrong with my code? I put it in python visualizer it worked out fine. Album = namedtuple('Album', 'id artist title year songs') Song =…
Alex Hu
  • 37
  • 7
-1
votes
4 answers

How to display a list of tuples and count the same number of elements

input def inisial(daftar): daftar = ( "Michael","Viny","Aurelio","Michael", "Felix","Kevin","Vincen","Vincen","Michael") inisial(daftar) output: Michael Viny Aurelio Michael2 Felix Kevin Vincen2 Vincen3 Michael3
Nothing
  • 11
  • 5
-1
votes
1 answer

How to deal with parsing an arbitrary number of lists into a dictionary

I am parsing an XMI/XML data structure into a pandas dataframe by first decomposing it into a dictionary. When I encounter a named tuple in a list in my XMI, there appear to be a maximum of two named tuples in my list (although the majority only…
horcle_buzz
  • 2,101
  • 3
  • 30
  • 59
-1
votes
1 answer

How to check if NamedTuple is in list?

I tried to check if an inctance of a NamedTuple "Transition" is equal to any object in the list "self.memory". Here is the code I tried to run: from typing import NamedTuple import random import torch as t Transition = NamedTuple('Transition',…
Luke C
  • 1
  • 1
-1
votes
1 answer

Changing Every Instance of a tuple

I am trying to WRITE A FUNCTION to change the every instance in a list of tuples. Basically i need to convert the every instance of the list from ('value', number, 'value') to Arc('value', number, 'value') Input: [('root', 1, 'a'), ('b', 0.0,…
-1
votes
1 answer

save a named tuple in all rows of a pandas dataframe

I'm trying to save a named tuple n=NamedTuple(value1='x'=, value2='y') in a row of a pandas dataframe. The problem is that the named tuple is showing a length of 2 because it has 2 parameters in my case (value1 and value2), so it doesn't fit it into…
Nickpick
  • 6,163
  • 16
  • 65
  • 116
-1
votes
2 answers

Namedtuple greater than or less than operators giving unexpected result

When working with namedtuples, it seems there is a default "value" for the object allowing one to compare two named tuples with the < > operators. Can anyone explain where this value comes from or why this code returns True? is there a clever way to…
wolfgee91
  • 13
  • 6
-1
votes
1 answer

Returning Max value grouping by N attributes

I am coming from a Java background and learning Python by applying it in my work environment whenever possible. I have a piece of functioning code that I would really like to improve. Essentially I have a list of namedtuples with 3 numerical values…