Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - building sublist that meets certain criteria from huge number of combinations
    primarykey
    data
    text
    <p>Been reading a long time, first time I have not been able to find an answer to something I'm working on. </p> <p>I have a list of 93 strings which are each 6 characters long. From those 93 strings, I want to identify a set of 20 which all meet a particular criteria relative to the others in the set. While itertools.combinations will give me all possible combinations, not all conditions are worth checking. </p> <p>For instance if [list[0], list[1], etc] fails because list[0] and list[1] can not be together it doesn't matter what the other 18 strings are, the set will fail every time, and that is a ton of wasted checking.</p> <p>Currently I have this working with 20 nested for loops, but it seems like there has to be a better/faster way to do it.:</p> <pre><code>for n1 in bclist: building = [n1] n2bclist = [bc for bc in bclist if bc not in building] for n2 in n2bclist: #this is the start of what gets repeated 19 times building.append(n2) if test_function(building): #does set fail? (counter intuitive, True when fail, False when pass) building.remove(n2) continue n3bclist = [bc for bc in bclist if bc not in building] #insert the additional 19 for loops, with n3 in n3, n4 in n4, etc building.remove(n2) </code></pre> <p>There are print statements in the 20th for loop to alert me if a set of 20 even exists. The for statements at least allow me to skip sets early when the single addition fails, but there is no memory of when larger combinations fail:</p> <p>For instance <code>[list[0], list[1]]</code> fails, so skip to <code>[list[0], [list[2]]</code> which passes. Next is <code>[list[0], list[2], list[1]]</code> which will fail because 0 and 1 are together again so it will move to <code>[list[0], list[2], list[3]]</code> which may or not pass. My concern is that eventually it will also test: </p> <ul> <li><code>[list[0], list[3], list[2]]</code></li> <li><code>[list[2], list[0], list[3]]</code></li> <li><code>[list[2], list[3], list[0]]</code></li> <li><code>[list[3], list[0], list[2]]</code></li> <li><code>[list[3], list[2], list[0]]</code></li> </ul> <p>All of these combinations will have the same outcome as the previous ones. Basically I trade the devil of itertools.combinations testing all combinations of sets which I know fail because of early values which fail for the devil of for loops which treat order of values as a factor when I do not care about their order. Both methods significantly increase the time it will take for my code to complete. </p> <p>Any ideas on how to get rid of the devils would be greatly appreciated.</p>
    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