3,143 questions
0
votes
1
answer
158
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) ...
4
votes
2
answers
95
views
Is there a way to use itertools with conditions for sets and arbitrary length?
I'm trying to create a list of subsets that add to a particular sum (e.g. 12), but with a limit on the occurrences of some of the terms.
The base set to iterate on is {1,2,3,4,5,6,7,8}. I'm trying to ...
3
votes
4
answers
217
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 ...
2
votes
2
answers
251
views
How can I remove only consecutive duplicate elements from a larger list in Python? [duplicate]
I have a list of elements where I want to remove only consecutive duplicates, not all duplicates.
For example:
data = [1, 1, 2, 2, 2, 3, 1, 1]
I want to get:
[1, 2, 3, 1]
This is different from using ...
5
votes
2
answers
185
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 ...
1
vote
1
answer
208
views
Create all possible outcomes from numpy matrices that show disjoints and subsets
Let's assume there is an event and within this event there are multiple outcomes that can co-exist.
An example would be a tennis game where A plays against B and B serves. A couple of possible ...
2
votes
1
answer
185
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 ...
3
votes
2
answers
118
views
What's the fastest way of skipping tuples with a certain structure in a itertool product?
I have to process a huge number of tuples made by k integers, each ranging from 1 to Max_k.
Each Max can be different.
I need to skip the tuples where an element has reached is max value, in that case ...
1
vote
0
answers
72
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
votes
1
answer
37
views
Tupe video clips list, the other matching durations. Itertools can find triplets that total 90 sec from duration list. Want to print clip names
I can get the triplets with iteration tools in durations list but I want to get matching clips from spots list. Tried to marry two list but dont know how to apply iteration combo on one part of the ...
1
vote
2
answers
67
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 ...
-2
votes
2
answers
79
views
Python itertools product aborting
When running the code below, python aborts and closes the python prompt:
>>> import itertools
>>> nComb = 2
>>> t = range(nComb)
>>> combs = list(itertools.product(...
1
vote
1
answer
125
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], ...]):
...
...
2
votes
0
answers
146
views
Efficiently Handling Large Combinations in Polars Without Overloading RAM
I have a list of n values (in my case, n=19), and I want to generate all possible combinations of these values. My goal is to use each combination as a filter for a Polars DataFrame, iterate over the ...
1
vote
1
answer
119
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 ...