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
2
votes
0 answers

Proper way to document that a class implements a collections interface?

Assuming I have a class that implements the Sequence interface, i.e. has __len__ and __getitem__ methods, what is the recommended way to document this? Is an informal description in the classes docstring enough, should I subclass…
gmolau
  • 2,815
  • 1
  • 22
  • 45
2
votes
1 answer

Which method is called when adding a key to a dict?

I would like to inherit the OrderedDict class to set up a maximum length to the dict. I did : from collections import OrderedDict class limitedDict(OrderedDict): def __init__(self, length): OrderedDict.__init__(self) …
Romain Jouin
  • 4,448
  • 3
  • 49
  • 79
2
votes
3 answers

retrieve input of collections.Counter output

Not sure if the title is correct but. Lets say you have a list that would look like the output from a Counter object. [(-3.0, 4), (-2.0, 1), (-1.0, 1), (0.0, 1), (1.0, 1), (2.0, 1), (3.0, 4)] How could I go back and get the original list, as [-3.0,…
Isbister
  • 906
  • 1
  • 12
  • 30
2
votes
1 answer

Why am I getting and adding behavior on a recursive method?

I am trying to generate XML with xmltodict. To do this I need to generate structures based on OrderedDict. I get the expected behavior the first time I execute my ".xml()" method but when I execute a second time I get the children elements added a…
luisfernando
  • 117
  • 1
  • 4
1
vote
1 answer

Test if collections.Mapping is equal to other mapping, or dict

I have written a custom mapping class that inherits from collections.Mapping. Is there an easy way to test if an instance's data is equal to another mapping object, e.g. dict?
Gary van der Merwe
  • 9,134
  • 3
  • 49
  • 80
1
vote
1 answer

Create a dictionary by entering a word and its meaning then print it at the end

This is the question: and this the output that is required: This is code I have written so far: class_list = [] keys = [] meanings = [] def script(): key = input("Enter the word: ") no_meanings = int(input("\nEnter the no of meanings:…
carlos
  • 45
  • 4
1
vote
1 answer

Python list comprehension optimization

I have used during a problem this piece of code to retrieve the count of each element in a list : nums = [1,1,3,2,3,3,4,1,1,4,2,3,3,2,1,3,4,1,2] print([nums.count(num) for num in set(nums)]) This code works well but doesn't look like to be as…
Lukas Laudrain
  • 436
  • 3
  • 10
1
vote
2 answers

max on collections.Counter

The max on collections.Counter is counter intuitive, I want to find the find the character that occurs the most in a string. >>> from collections import Counter >>> c = Counter('aaaabbbcc') >>> max(c) 'c' >>> c Counter({'a': 4, 'b': 3, 'c': 2}) I…
Data Cyclist
  • 21
  • 1
  • 3
1
vote
1 answer

Sequence vs. MutableSequence

I've been using Sequence in type hints for sequences including mutable lists. Now I've just discovered that there is also MutableSequence. As far as I can tell, Sequence is a superclass of MutableSequence, i.e., Sequence includes both mutables like…
flotzilla
  • 1,181
  • 1
  • 13
  • 23
1
vote
1 answer

Python 3.9.5: One dictionary assignment is overwriting multiple keys [BUG?]

I am reading a .csv called courses. Each row corresponds to a course which has an id, a name, and a teacher. They are to be stored in a Dict. An example: list_courses = { 1: {'id': 1, 'name': 'Biology', 'teacher': 'Mr. D'}, ... } While…
KuboMD
  • 684
  • 5
  • 16
1
vote
1 answer

Way to achieve str.strip()-like behaviour with iterable Python-objects

Which is the way is to achieve str.strip()-like behaviour with list, tuple and similar iterable objects in Python? Examples: str.strip()-like >>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t'] >>> list_strip(lst) ... [0, 'a', ' ', 0] >>>…
dinya
  • 1,563
  • 1
  • 16
  • 30
1
vote
2 answers

How to access elements in a dict_values?

I have a dict : {49: {'created_at': '2018-11-07T13:25:12.000Z', 'url': 'https://www.test.com'}} I would like to get 'created_at'. I've attempt through different methods but without a success... I thought this approach would works: result =…
1
vote
1 answer

python counting elements in iterable with filter

To count the elements in a list, you can use collections.Counter, but what if only some of the elements have to be counted? I've set up this example (please note: numpy is just for convenience. In general the list will contain arbitrary python…
lhk
  • 27,458
  • 30
  • 122
  • 201
1
vote
3 answers

Convert 2-column counter-like csv file to Python collections.Counter?

I have a comma separated (,) tab delimited (\t), file. 68,"phrase"\t 485,"another phrase"\t 43, "phrase 3"\t Is there a simple approach to throw it into a Python Counter?
GollyJer
  • 23,857
  • 16
  • 106
  • 174
1
vote
1 answer

Why do I have to instantiate a python deque of namedtuples as a list?

I have a queue of nodes that I need to have an upper and lower bound to them, so I have a named tuple called QueueEntry. QueueEntry = collections.namedtuple('QueueEntry', ('node', 'lower', 'upper')) When I instantiate a deque with the named tuple…
user22261
  • 47
  • 7
1 2 3
8 9