A Python module that implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.
Questions tagged [python-collections]
129 questions
1
vote
0 answers
nested defaultdict: TypeError: first argument must be callable or None
Truing to create a nested defaultdict to avoid having to write long code.
The final defaultdict should looks like this:
dd = {str:{str:{str:{[]}}}}
But when I try to append item into the list it returns TypeError:
dd = defaultdict(lambda:…

Milano
- 18,048
- 37
- 153
- 353
1
vote
0 answers
Optimal way to ensure data integrity in Python
tl;dr:
In Python is there a performant means of data integrity validation?
Synopsis:
I'm working on a framework that (A) ingests similar types of data from multiple API services that can be built out by developers, (B) allows users/developers to…

Bobby
- 1,439
- 2
- 16
- 30
1
vote
1 answer
How to split a deque in two
I'm writing something where I am popping and appending often and thought it would be appropriate to use deque. However, somewhere in my code I need to divide the deque in two.
Consider the deque d
from collections import deque
d =…

piRSquared
- 285,575
- 57
- 475
- 624
1
vote
2 answers
How to calculate variables from worksheet columns using xlrd?
I am attempting to calculate all variables of a specific value in a given column from an Excel document. I want to be able to iterate over the column and calculate the total of each instance... e.g. how many students received a grade "A".
Here is…

John Doe
- 13
- 3
1
vote
1 answer
How can I port this code using collections.deque from Python 2.5 to 3.4?
How can you port this code from 2.5 to 3.4 version?
from __future__ import with_statement
from ..globals import addonPath
import os, time
import collections
class TailDeque(collections.deque):
'''Implementation of deque with limited maxlen…

Fragkiller
- 589
- 2
- 8
- 21
1
vote
5 answers
How to get the same result in book "Web Scraping with Python: Collecting Data from the Modern Web" Chapter 7 Data Normalization section
Python version: 2.7.10
My code:
# -*- coding: utf-8 -*-
from urllib2 import urlopen
from bs4 import BeautifulSoup
from collections import OrderedDict
import re
import string
def cleanInput(input):
input = re.sub('\n+', " ", input)
input =…

Runze Sun
- 61
- 4
1
vote
2 answers
Finding combinations that meet a threshold relation
Given a value for phi, theta, n_1, and n_2, I need to find all possible pairs (N_1, N_2) that meet the following criteria:
0 <= N_1 <= n_1
0 <= N_2 <= n_2
N_1 - phi * N_2 >= theta
What is the most efficient way to do this in Python? Obviously I…

abcd
- 10,215
- 15
- 51
- 85
1
vote
1 answer
How/When does Python garbage collect objects which contain a collection of all of their own type?
I am working on a Python class that is structured like the example in this answer: https://stackoverflow.com/a/1383744/576333. The class itself is keeping track of all created objects using a dictionary.
class Repository(object):
# All…

Randy
- 908
- 12
- 30
0
votes
1 answer
itertools dice rolls: doubles roll twice
I'm trying to learn the Python library itertools and I thought a good test would be the simulation of dice rolls. It's easy to generate all possible rolls using product and counting the number of possible ways of doing so with the collections…

Hooked
- 84,485
- 43
- 192
- 261
0
votes
0 answers
Error with Python Collections Module in __init__.py when using ML Ensemble (mlens)
I am trying to use ML Ensemble. You can see installation details here: http://ml-ensemble.com/info/start/install.html.
I would run in terminal:
pip3 install sklearn
pip3 install mlens
pip3 install pandas
pip3 install -U mlens
and I have Python…
0
votes
2 answers
Import error with named tuple and Mapping
I have trying to fix an import error with the requests library in Python 3.11. This is to run a Discord bot that I have created. My original error is given as ImportError: cannot import name 'Mapping' from 'collections' and in response I have tried…

mullena
- 58
- 1
- 8
0
votes
1 answer
How do i get top of the stack implemented using collections.deque
I am doing the Leetcode valid parenthesis question and want to implement the ans using deque stack.
there are three types of parenthesis {[()]} and i need to check the top of the stack before i pop it.
i couldnt find any method for collections.deque…
0
votes
0 answers
Code module returning "unhashable type: list" error when sending result through Fast API
I've written a python code to check the unique values of the specified column names from a pandas dataframe.
Main Code:
Checking the unique values & the frequency of their occurence
def uniq_fun(df, col_name):
try:
…

Apoorva
- 75
- 6
0
votes
1 answer
Inheriting from type and typing.Mapping: "TypeError: descriptor '__subclasses__' of 'type' object needs an argument"
I am trying to define a class that is supposed to simultaneously do two things:
serve as the metaclass for a dataclass
act like a mapping
i.e., it will need to be derived from both type and typing.Mapping.
Defining such a class itself works, but I…
0
votes
1 answer
list of tuples to single list
I have list of tuples in the following format,
[('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]
I need to convert them into following format,
[['ABC','32064','Woo'],
['ABC','32064','MISSI'],
['XYZ','32065','HAY']]
I have tried…

code_bug
- 355
- 1
- 12