Note that there are some explanatory texts on larger screens.

plurals
  1. POYield sub combinations with limit
    text
    copied!<p>I'm working with python 3. The function I'm working with is as follows: </p> <pre><code>def sub_combinations(segment): if len(segment) == 1: yield (segment,) else: for j in sub_combinations(segment[1:]): yield ((segment[0],),)+j for k in range(len(j)): yield (((segment[0],)+j[k]),) + (j[:k]) +(j[k+1:]) </code></pre> <p>its a version of this function:</p> <p><a href="https://stackoverflow.com/questions/8646186/yielding-sub-combinations">Yielding sub combinations</a></p> <p>the output is as follows for (1,2,3,4,5):</p> <pre><code>((1,), (2,), (3,), (4,), (5,)) ((1, 2), (3,), (4,), (5,)) ((1, 3), (2,), (4,), (5,)) ((1, 4), (2,), (3,), (5,)) * ((1, 5), (2,), (3,), (4,)) * ((1,), (2, 3), (4,), (5,)) ((1, 2, 3), (4,), (5,)) ((1, 4), (2, 3), (5,)) * ((1, 5), (2, 3), (4,)) * ((1,), (2, 4), (3,), (5,)) ((1, 2, 4), (3,), (5,)) ((1, 3), (2, 4), (5,)) ((1, 5), (2, 4), (3,)) * ((1,), (2, 5), (3,), (4,)) * ((1, 2, 5), (3,), (4,)) * ((1, 3), (2, 5), (4,)) * ((1, 4), (2, 5), (3,)) * ((1,), (2,), (3, 4), (5,)) ((1, 2), (3, 4), (5,)) ((1, 3, 4), (2,), (5,)) ((1, 5), (2,), (3, 4)) * ((1,), (2, 3, 4), (5,)) ((1, 2, 3, 4), (5,)) ((1, 5), (2, 3, 4)) * ((1,), (2, 5), (3, 4)) * ((1, 2, 5), (3, 4)) * ((1, 3, 4), (2, 5)) * ((1,), (2,), (3, 5), (4,)) ((1, 2), (3, 5), (4,)) ((1, 3, 5), (2,), (4,)) ((1, 4), (2,), (3, 5)) * ((1,), (2, 3, 5), (4,)) ((1, 2, 3, 5), (4,)) ((1, 4), (2, 3, 5)) * ((1,), (2, 4), (3, 5)) ((1, 2, 4), (3, 5)) ((1, 3, 5), (2, 4)) ((1,), (2,), (3,), (4, 5)) ((1, 2), (3,), (4, 5)) ((1, 3), (2,), (4, 5)) ((1, 4, 5), (2,), (3,)) * ((1,), (2, 3), (4, 5)) ((1, 2, 3), (4, 5)) ((1, 4, 5), (2, 3)) * ((1,), (2, 4, 5), (3,)) ((1, 2, 4, 5), (3,)) ((1, 3), (2, 4, 5)) ((1,), (2,), (3, 4, 5)) ((1, 2), (3, 4, 5)) ((1, 3, 4, 5), (2,)) ((1,), (2, 3, 4, 5)) ((1, 2, 3, 4, 5),) </code></pre> <p>The problem is that if I work with larger tuples, the function sub_combinations returns a huge amount of data and takes too long to compute it. To address this, I want to limit the amount of data returned by adding an extra argument. For example, sub_combinations((1,2,3,4,5), 2) should return the data above but without the tuples marked with a star. These are dropped because the offset between consequtive values in the tuple is greater than 2. For example, rows containing (1, 4), (1, 5) or (2, 5) and the likes of (1, 2, 5) etc, are dropped. </p> <p>The line </p> <pre><code>for k in range(len(j)) </code></pre> <p>needs to be adjusted to drop these lines, but I haven't figured out yet how. Any suggestions?</p> <p>Barry </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload