3,144 questions
0 votes
1 answer
144 views
Advanced combinatorics in Python: binom(n,2) subsets of binom(n,3) combinations without repetition
I have a list d_n of n integers and the results Z_klm of a function fun(dk, dl, dm) for all binom(n, 3) combinations without repetition (k, l, m) out of d_n indices. Now, for all binom(n, 2) ...
3 votes
4 answers
206 views
Cartesian product for both keys and values of a dictionary?
I need to get the Cartesian product of a dictionary {str: Fraction} with itself, but currently need to "loop" through the dict twice, once for the keys and once for the values. The ...
5 votes
2 answers
160 views
Performance of list.extend slice vs islice
It seems that even when islice would theoretically be better, in practice, it is slower than just using slice. So I am a bit puzzled by the difference in performance between the usage of slice and ...
2 votes
1 answer
179 views
How to create possible sets of n numbers from m-sized prime number list?
Input: a list of m prime numbers (with possible repetition), and integers n and t. Output: all sets of n numbers, where each set is formed by partitioning the input into n parts, and taking the ...
1 vote
0 answers
67 views
How do I use a custom mapping (sort) on my itertools for loop so that I can print specific strings first?
I'm using python itertools specifically to generate combinations of integer strings. I have already created the loop that will loop through the combinations one at a time. I just need someone to help ...
1 vote
2 answers
60 views
Dealing with `StopIteration` return from a next() call in Python
I'm using the below to skip a group of records when a certain condition is met: if (condition met): ... [next(it) for x in range(19)] Where it is an itertuples object created to speed up ...
1 vote
1 answer
96 views
Type hint for itertools.product doesn't know length of elements
I am adding type hints to an old code of mine. However, I find with a "problem" I don't know how to solve. I have a function that looks like: def f(x: tuple[tuple[int, int], ...]): ... ...
1 vote
1 answer
111 views
Is there an iterator in Python that gives the product but omits permutations of the classes itself?
Assume that I want to assign a color to 5 different balls and have 3 colors r,b and g. I would like to iterate over all these combinations. Preferably I would want to omit combinations that are equal ...