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
0
votes
1 answer

`dict.pop` ignores the default value set by `collections.defaultdict(default_factory)`

Python's dict.pop(key[, default]) ignores the default value set by collections.defaultdict(default_factory) as shown by the following code snippet: from collections import defaultdict d = defaultdict(lambda: 1) d['a'] d['b'] = 2 print(d.pop('a')) #…
0
votes
1 answer

Python runtime error with dictionary for loop when appending value list

I am trying to make a contact list/phone book in python 3.10 with a dictionary and list and have been trying to add a feature to add a searched contact that isn't inside of the phone book. The current method I am trying gives me "Runtime Error:…
0
votes
1 answer

Use namedtuple instead of tuple with typing.optional

How can I use namedtuple with typing.optional instead of this tuple ? I want to to call the function in the format of result_final(power=Stats(min=12, max=None)) Thank you. I tried with Stats = namedtuple('Stats', [Optional[int],…
user17572769
0
votes
2 answers

How to use proper container in python

For the following code I would like to use a structure in order to not have to give 2 values for each element min and max and also I would like to have a container if exists to can give one value and the other one to remain None. For instance power…
user17572769
0
votes
2 answers

How to store a collection of constants that can be called by Collection.variable?

I'm searching for a collection that can be used to store multiple constants and also can be used to get a list of them? EDIT: It does not have to be constants, it can be variables. I want to get a value this way: Constants.first_one But also get a…
Milano
  • 18,048
  • 37
  • 153
  • 353
0
votes
1 answer

What's the time complexity of Python's collections.Counter.total()?

What's the time complexity of Python's collections.Counter.total()? I've read the documentation for the method, but there's not mention of its efficiency. Does anyone know how the method is implemented under the hood and what its time complexity…
FountainTree
  • 316
  • 1
  • 11
0
votes
0 answers

Get list items that don't match most frequent value

I have a list of dictionaries looking like this: [{'customer': 'Charles', 'city': 'Paris'}, {'customer': 'John', 'city': 'New York'}, {'customer': 'Jean', 'city': 'Paris'}] I tried something using collections which will return me the name of the…
Synops
  • 122
  • 3
  • 11
0
votes
1 answer

What is the difference between getattr() and calling the attribute?

Here is a code I have written. I presumed both of them to return the same answer but they don't! How are they different? from collections import deque d = deque() for _ in range(int(input())): method, *n = input().split() getattr(d,…
0
votes
2 answers

Python append to list replacing all previous indexes with last value

In the following Python 3 code, the correct value is written into the daysSchedule but when iterating to the next value. class Data: def getDaysSchedule(scheduleUrl, filterTeam = ''): with urllib.request.urlopen(scheduleUrl) as…
eat-sleep-code
  • 4,753
  • 13
  • 52
  • 98
0
votes
1 answer

TypeError: 'type' object is not iterable when iterating over collections.deque that contains collections.namedtuple

I made a simple replay buffer that when I sample from it gives me the error TypeError: 'type' object is not iterable import collections import numpy as np Experience = collections.namedtuple("Experience", field_names=["state", "action", "reward",…
0
votes
1 answer

How to access internal dictionary of collection.Counter to extend the functionality of Child class by using Inheritance?

I want to add 2 functions in collections.Counter to add functionality to it for sorting and returning minimum n values. For this to work, I need to access the internal dictionary and I don't know how to use it. I checked the internal variables for…
Deshwal
  • 3,436
  • 4
  • 35
  • 94
0
votes
3 answers

How to use Counter in python without showing numbers

I have little problem with python's Counter library. Here is an example: from collections import Counter list1 = ['x','y','z','x','x','x','y', 'z'] print(Counter(list1)) Output of it: Counter({'x': 4, 'y': 2, 'z': 2}) My question is how to receive…
Osho
  • 168
  • 1
  • 9
0
votes
1 answer

In Python why does the following code give incorrect answers when the list is sorted and list is un-sorted?

I am trying to sort a list based on the frequency of it's elements. But I get two different answers when the list is sorted and list is un-sorted. Please see the code segment below. Could someone explain the cause. Thank you. from collections import…
Ram
  • 4,724
  • 2
  • 14
  • 22
0
votes
1 answer

Class Initialization of a list of empty lists not updating

I have a list that is made up of 4 deques with a set length. I have made that list with the hope that it will update as the other ones. Here is a simple example: class foo(): def __init__(self): self.buffer_size = 300 self.a =…
0
votes
0 answers

Python `UserString` seems problematic?

I need to use UserString to create my own str class, but its implementation seems problematic. For example, in the class definition, it reads: def __eq__(self, string): if isinstance(string, UserString): return self.data ==…
vanbastelaer
  • 368
  • 2
  • 15
1 2 3
8 9