Questions tagged [python-collections]

A Python module that implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

129 questions
6
votes
3 answers

most common 2-grams using python

Given a string: this is a test this is How can I find the top-n most common 2-grams? In the string above, all 2-grams are: {this is, is a, test this, this is} As you can notice, the 2-gram this is appears 2 times. Hence the result should be: {this…
stfd1123581321
  • 163
  • 1
  • 2
  • 6
5
votes
3 answers

Cast dict to defaultdict

The following code uses the {} operator to combine two defaultdicts. from collections import defaultdict aa=defaultdict(str) bb=defaultdict(str) aa['foo']+= '1' bb['bar']+= '2' cc = {**aa,**bb} type(cc) But, as we see if we run this, the {}…
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
5
votes
2 answers

Counter allowing repetitions

I have a Traffic Light Enum defining possible states: class TrafficLightPhase(Enum): RED = "RED" YELLOW = "YELLOW" GREEN = "GREEN" I poll a Traffic Light to get current state every second and I put the values in a deque with this…
logoff
  • 3,347
  • 5
  • 41
  • 58
5
votes
3 answers

python multiply two collection counters

Python collection counter Curious if there is a better way to do this. Overriding a Counter class method? The built-in multiply produces the dot product of two counters from collections import Counter a = Counter({'b': 4, 'c': 2, 'a': 1}) b =…
5
votes
1 answer

Why is collections.Counter uppercase and collections.defaultdict is not?

Some of the elements in the collections module seem to be uppercase, some other not. Is there a specific rationale behind it?
meto
  • 3,425
  • 10
  • 37
  • 49
5
votes
2 answers

collections.Counter: most_common INCLUDING equal counts

In collections.Counter, the method most_common(n) returns only the n most frequent items in a list. I need exactly that but I need to include the equal counts as well. from collections import Counter test =…
KarelCote
  • 53
  • 1
  • 4
5
votes
3 answers

Python Counter Comparison as Bag-type

I need a bag/multiset-like data type in Python. I understand collections.Counter is often used for this purpose. But the comparison operators don't seem to work: In [1]: from collections import Counter In [2]: bag1 = Counter(a=1, b=2, c=3) In [3]:…
William Reed
  • 101
  • 4
5
votes
2 answers

Make Counter.most_common return dictionary

I used the sample from the documentation: >>> Counter('abracadabra').most_common(3) [('a', 5), ('r', 2), ('b', 2)] How can I make the result to be: { 'a': 5, 'r' :2 , 'b' :2} supposing that we want to keep the Counter().most_common() code?
Diolor
  • 13,181
  • 30
  • 111
  • 179
4
votes
3 answers

Algorithmic complexity to convert a set to a list in python

In python, when I convert my set to a list, what is the algorithmic complexity of such a task? Is it merely type-casting the collection, or does it need to copy items into a different data structure? What's happening? I'd love to learn that the…
macetw
  • 1,640
  • 1
  • 17
  • 26
4
votes
1 answer

key function for heapq.nlargest()

I have a dictionary with {key: count}, say status_count = {'MANAGEMENT ANALYSTS': 13859, 'COMPUTER PROGRAMMERS': 72112} and I am trying to write a key function for heapq.nlargest() that sorts based on count and if there are ties I have to sort based…
pulsar
  • 141
  • 2
  • 13
4
votes
2 answers

Python append Counter to Counter, like Python dictionary update

I have 2 Counters (Counter from collections), and I want to append one to the other, while the overlapping keys from the first counter would be ignored. Like the dic.update (python dictionaries update) For example: from collections import Counter a…
sheldonzy
  • 5,505
  • 9
  • 48
  • 86
4
votes
4 answers

python set union operation is not behaving well with named tuples

I want to create a set of namedtuple in python, with the ability to add elements dynamically using the union operation. The following code snippet creates a set of namedtuple, which is behaving nicely. from collections import namedtuple B =…
Jayendra Parmar
  • 702
  • 12
  • 30
4
votes
2 answers

Inherit from collections.Counter: 'fromkeys' is abstract

I have a python class that inherit from collections.Counter: class Analyzer(collections.Counter): pass When I use pylint on this code, its answer is: W: Method 'fromkeys' is abstract in class 'Counter' but is not overridden…
audeoudh
  • 1,279
  • 1
  • 8
  • 23
4
votes
4 answers

Print first Key Value in an Ordered Counter

I am trying to print out the Key Value pair in the same order as displayed in the OrderedCounter Output. from collections import Counter, OrderedDict class OrderedCounter(Counter, OrderedDict): pass c = OrderedCounter('supernatural') print…
Vishwak
  • 323
  • 3
  • 11
4
votes
1 answer

Why doesn't OrderedDict use super?

We can create an OrderedCounter trivially by using multiple inheritance: >>> from collections import Counter, OrderedDict >>> class OrderedCounter(Counter, OrderedDict): ... pass ... >>> OrderedCounter('Mississippi').items() [('M', 1), ('i',…
wim
  • 338,267
  • 99
  • 616
  • 750
1
2
3
8 9