133 questions
5
votes
1
answer
108
views
UserDict.popitem is not LIFO
Consider the following snippet:
from collections import UserDict
class D(dict): ...
d = D(foo="bar", baz=42)
print(d.popitem()) # ('baz', 42)
class UD(UserDict): ...
ud = UD(foo="...
1
vote
1
answer
152
views
Python raw material inventory optimizer
I want to build a function that can look at the raw material on hand information I provide, then come up with combinations of inputs to achieve a desired number of finished products.
on_hand_inventory ...
-1
votes
1
answer
133
views
Implement the __getitem__ method of a minimal collections.abc sequence with type hints
How does a minimal implementation of a Sequence from collections.abc, together with the correct type hints, look like?
According to its documentation, __len__ and __getitem__ are sufficient. My type ...
0
votes
1
answer
122
views
Is there a better way to zip iterables with mask?
I need to zip iterables with a personal mask function or just mask lists [True, False, ...]
from collections.abc import Callable
def zip_mask(a, b, mask):
iter_a, iter_b = iter(a), iter(b)
if ...
2
votes
1
answer
656
views
How to convert AsyncIterable to asyncio Task
I am using Python 3.11.5 with the below code:
import asyncio
from collections.abc import AsyncIterable
# Leave this iterable be, the question is about
# how to use many instances of this in parallel
...
0
votes
2
answers
2k
views
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' ...
0
votes
1
answer
1k
views
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....
0
votes
0
answers
147
views
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(...
0
votes
1
answer
160
views
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....
2
votes
1
answer
75
views
Fast way to create a nested dictionary from a list of tuples without a for loop
I know similar questions have been asked before but I cannot find an adequate answer to my situation.
Let's say I have the following list of tuples:
d = [('first', 1), ('second', 2), ('third', 3)]
I ...
0
votes
1
answer
100
views
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',...
-1
votes
2
answers
198
views
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 ...
1
vote
1
answer
581
views
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: ")
...
1
vote
1
answer
318
views
`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(...
-5
votes
1
answer
49
views
Why iterating on a collection does not require index? [duplicate]
I'm trying to sort my confusions learning python.
>>> cities = ['London', "Toronto", 'Paris', 'Oslo']
>>> cities
['London', 'Toronto', 'Paris', 'Oslo']
>>> for i ...