Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction for conditional for loop
    primarykey
    data
    text
    <p>Ok, this is the complete code that I'm working with. Now I've provided input and output. Hope it makes more sense that my absurd questions earlier.....</p> <pre><code>def get_positions(xs, item): if isinstance(xs, list): for i, it in enumerate(xs): for pos in get_positions(it, item): yield (i,) + pos elif xs == item: yield () blocks = [-12,-10,-8,-6,-4,-2,0,2,4,6,8,10,12,14,16] startcombos = [[-12], [-12, -10], [-12, -10, -8], [-12, -10, -8, -6], [-12, -10, -8, -6, -4], [-12, -10,-8, -6, -4, -2], [-12, -10, -8, -6, -4, -2, 0], [-12, -10, -8, -6, -4, -2, 0, 2], [-12, -10, -8, -6, -4, -2, 0, 2, 4], [-12, -10, -8, -6, -4, -2, 0, 2, 4, 6], [-12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8], [-12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10], [-12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12], [-12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14], [-12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16]] combos = [[-10], [-10, -8], [-10, -8, -6], [-10, -8, -6, -4], [-10, -8, -6, -4, -2], [-10, -8, -6,-4, -2, 0], [-10, -8, -6, -4, -2, 0, 2], [-10, -8, -6, -4, -2, 0, 2, 4], [-10, -8, -6, -4, -2, 0, 2, 4, 6], [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8], [-10, -8, -6, -4, -2, 0, 2, 4, 6,8, 10], [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12], [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14], [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16], [-8], [-8, -6], [-8, -6, -4], [-8, -6, -4, -2], [-8, -6, -4, -2, 0], [-8, -6, -4, -2, 0, 2], [-8, -6, -4, -2, 0, 2, 4], [-8, -6, -4, -2, 0, 2, 4, 6], [-8, -6, -4, -2, 0, 2, 4, 6, 8], [-8, -6, -4, -2, 0, 2, 4, 6, 8, 10], [-8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12], [-8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14], [-8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16], [-6], [-6, -4], [-6, -4, -2], [-6, -4, -2, 0], [-6, -4, -2, 0, 2], [-6, -4, -2, 0, 2, 4], [-6, -4, -2, 0, 2, 4, 6], [-6, -4, -2, 0, 2, 4, 6, 8], [-6, -4, -2, 0, 2, 4, 6, 8, 10], [-6, -4, -2, 0, 2, 4, 6, 8, 10, 12], [-6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14], [-6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16], [-4], [-4, -2], [-4, -2, 0], [-4, -2, 0, 2], [-4, -2, 0, 2, 4], [-4, -2, 0, 2, 4, 6],[-4, -2, 0, 2, 4, 6, 8], [-4, -2, 0, 2, 4, 6, 8, 10], [-4, -2, 0, 2, 4, 6, 8, 10, 12], [-4, -2, 0, 2, 4, 6, 8, 10, 12, 14], [-4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16], [-2], [-2, 0],[-2, 0, 2], [-2, 0, 2, 4], [-2, 0, 2, 4, 6], [-2, 0, 2, 4, 6, 8], [-2, 0, 2, 4, 6, 8, 10], [-2, 0, 2, 4, 6, 8, 10, 12], [-2, 0, 2, 4, 6, 8, 10, 12, 14], [-2, 0, 2, 4, 6, 8, 10, 12, 14, 16], [0], [0, 2], [0, 2, 4], [0, 2, 4, 6], [0, 2, 4, 6, 8], [0, 2, 4, 6, 8, 10], [0,2, 4, 6, 8, 10, 12], [0, 2, 4, 6, 8, 10, 12, 14], [0, 2, 4, 6, 8, 10, 12, 14, 16], [2], [2, 4], [2, 4, 6], [2, 4, 6, 8], [2, 4, 6, 8, 10], [2, 4, 6, 8, 10, 12], [2, 4, 6, 8, 10, 12, 14], [2, 4, 6, 8, 10, 12, 14, 16], [4], [4, 6], [4, 6, 8], [4, 6, 8, 10], [4, 6, 8, 10,12], [4, 6, 8, 10, 12, 14], [4, 6, 8, 10, 12, 14, 16], [6], [6, 8], [6, 8, 10], [6, 8, 10, 12], [6, 8, 10, 12, 14], [6, 8, 10, 12, 14, 16], [8], [8, 10], [8, 10, 12], [8, 10, 12, 14], [8, 10, 12, 14, 16], [10], [10, 12], [10, 12, 14], [10, 12, 14, 16], [12], [12, 14], [12, 14, 16], [14], [14, 16], [16]] temp = [] for i in range(len(startcombos)): for j in list(get_positions(combos,startcombos[i][-1]+2)): if j[-1]==0 and combos[j[0]][-1]!=blocks[-1]: for k in list(get_positions(combos,combos[j[0]][-1]+2)): if k[-1]==0 and combos[k[0]][-1]!=blocks[-1]: for l in list(get_positions(combos,combos[k[0]][-1]+2)): if l[-1]==0 and combos[l[0]][-1]==blocks[-1]: temp.append(tuple(startcombos[i])) temp.append(tuple(combos[j[0]])) temp.append(tuple(combos[k[0]])) temp.append(tuple(combos[l[0]])) combinations.append(temp) temp = [] </code></pre> <p>This is a code to generate temp of length 4. If the length of combinations increases, I would include another condition inside the bigger loop like this</p> <pre><code>for i in range(len(startcombos)): for j in list(get_positions(combos,startcombos[i][-1]+2)): if j[-1]==0 and combos[j[0]][-1]!=blocks[-1]: for k in list(get_positions(combos,combos[j[0]][-1]+2)): if k[-1]==0 and combos[k[0]][-1]!=blocks[-1]: for l in list(get_positions(combos,combos[k[0]][-1]+2)): if l[-1]==0 and combos[l[0]][-1]!=blocks[-1]: for m in list(get_positions(combos,combos[l[0]][-1]+2)): if m[-1]==0 and combos[m[0]][-1]==blocks[-1]: temp.append(tuple(startcombos[i])) temp.append(tuple(combos[j[0]])) temp.append(tuple(combos[k[0]])) temp.append(tuple(combos[l[0]])) temp.append(tuple(combos[m[0]])) combinations.append(temp) temp = [] </code></pre> <p>This will generate a temp with length 5. As you have noticed the first and last conditions and loops changes, the middle part remains the same. How to write a function or any other way that I can achieve it with variable lengths?</p> <p>Now what I would get here is basically combinations which is obtained by providing specific conditions. I know that I could try python combinations from itertools but generating all combinations is simply not worth it if I want specific combinations and besides computational time is also important. Thus, it would give the following output for length 4</p> <pre><code>combinations[0] = [[-12],[-10],[-8],[-6,-4,-2,0,2,4,6,8,10,12,14,16]] </code></pre> <p>and so on which are 364 in number.</p> <p>For length 5, it will give a total of 1001 combinations of which the first one is this</p> <pre><code>combinations[0] = [[-12],[-10],[-8],[-6],[-4,-2,0,2,4,6,8,10,12,14,16]] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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