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
0
votes
1 answer
Is there a way to access the underlying dict from a dict subclass?
I'd like to build a special dict class that has the option to export it's underlying dict as a whole (not just the individual items), i.e. something like this:
class CustomDict(dict):
def export(self):
return ??? # A dict instance
I…

gmolau
- 2,815
- 1
- 22
- 45
0
votes
1 answer
vector>> data structure in python
How to create an analogy of vector>> of C++ data structure in python? and sort it by the first (int) parameter. I tried to use the list of lists in python, but this is not what I want. Thank you.

Suleyman Suleymanzade
- 393
- 4
- 18
0
votes
0 answers
How to dynamically create namedtuple from a list of lists?
So I have a list of list called templist like this:
[['C0', 1, 1], ['NA', 4, 5], ['NA', 3, 7], ['C3', 9, 3], ['NA', 8, 4], ['NA', 7, 5], ['C6', 6, 6], ['NA', 5, 10]]
The way you create a namedtuple for the above list is:
from collections import…
user8508347
0
votes
2 answers
Python JSON load / decoding list of objects
I'm trying figure out how to load file called foo.json with this content:
[
{
"bar1": "foobar1-1",
"bar2": "foobar1-2",
"bar3": "foobar1-3"
},
{
"bar1": "foobar2-1",
"bar2": "foobar2-2",
…

Jason Thurston
- 347
- 5
- 7
0
votes
1 answer
Restricting a Counter Collection in Python
I am currently extracting data from a web API and I am trying to filter some values based on the following condition: Count the number of times that a user appears between two given dates but only if he/she has bought items raspberry pi, banana pi…

abautista
- 2,410
- 5
- 41
- 72
0
votes
1 answer
Removing All Spacing from HTML String
I'm trying to implement code that removes all white space and space characters then counts the top 3 alphanumeric characters occurring in the page. My question is twofold.
1) The method I'm using for split doesn't seem to be working and I'm not…

CFalco
- 19
- 5
0
votes
1 answer
How to create a List like class which allows calling contained objects methods on slices which work with both py2 and py3
The code below is from SCons's code base. We're working on porting the code such that it will work with both Python 2.7.x and 3.x.
The code below works fine under python 2.7.x, but when run under python 3.5 fails as follows:
python3.5…

bdbaddog
- 3,357
- 2
- 22
- 33
0
votes
0 answers
Python OrderedDict with lambda
Can we have an ordered dictionary, with some default value?
As in case of defaultdict, i can define something like:
a = collections.defaultdict(lambda:[True,True])
Can we have the above lambda functionality, but have elements inserted in the dict…

Shod
- 801
- 3
- 12
- 33
0
votes
1 answer
collections counter python can't access the key
I am using the collections counter to count each string (they might not be unique) inside lists. The problem is that now I can't access the dictionary, and I am not sure why.
My code is:
from collections import Counter
result1 =…

Gonzalo
- 1,084
- 4
- 20
- 40
0
votes
1 answer
Given a list of numbers, find a fix range that can cover the most numbers
in python, we can use counter to find the most common element in a list.
Is there a way we can pass in the function, so that we can counter the element that falls in certain range.
Say I have [123, 127, 99,75,86, 83,81], I want to return the…

9blue
- 4,693
- 9
- 29
- 43
0
votes
1 answer
return counter object in multiprocessing / map function
I have an python script running, that starts the same function in multiple threads. The functions creates and process 2 counters (c1 and c2). The result of all c1 counters from the forked processes should be merged together. Same to the results of…

The Bndr
- 13,204
- 16
- 68
- 107
0
votes
1 answer
TypeError : update() takes from 1 to 2 positional arguments but 3 were given.
The Counter.update() function expects two arguments (data and its key) and I have provided two arguments but it complains of having provided three arguments.
from collections import Counter
InputString1 = input()
InputString2 = input()
Set1 =…

priyadarshi1729
- 11
- 2
- 8
0
votes
1 answer
django / python collections issue
I have some code that I can not get my head around. The more I look at it the more confused I become.
There are two date values and a language code that is passed into a js function. Then there is a django collection (I think!) that interacts with…

user3354539
- 1,245
- 5
- 21
- 40
0
votes
2 answers
Why OrderedDict has this behavior
In Python 2.7 I am having this behavior with OrderedDict
from collections import *
id(OrderedDict())
42101904
id(OrderedDict())
42071680
id(OrderedDict())
42071680
id(OrderedDict())
42071680
id(OrderedDict())
42071680
Why?

luisfernando
- 117
- 1
- 4
-1
votes
2 answers
In python, how can I create & maintain a list of IP addresses indexable by a pair of IP addresses?
I'm looking for a way to maintain and update a list of IP addresses that I can index into (via a pair of IP addresses) to add a new address to the list.
For example, suppose I have an arbitrary index of (10.0.0.1 & 11.0.0.1). For that pair of…

wwwalker
- 31
- 8