python itertools skipping ahead -


i have list of lists. using itertools, doing

for result in product([a,b],[c,d],[e,f,g]): # test each result

and result desired product, each result containing 1 element each of lists. code tests each of results element-by-element, looking first (and best) 'good' one. there can very large number test.

let's i'm testing first result 'ace'. let's when test second element 'c' find 'ace' bad result. there no need test 'acf' or 'acg'. want skip failed ace directly trying ade. anyway without throwing unwanted results on floor?

if implementing nested loops, trying manipulate loop indexes inside loop , not nice ... want skip testing lot of results. can skip ahead efficiently in itertools?

itertools not best way go concern have.

if have 3 sets combine, loop on , when fail, break loops. (if code complex, set variable , break right outside.

for i1 in [a, b]:   i2 in [c, d]:       i3 in [e, f, g]:          if not test(i1, i2, i3):            break 

however, if number of sets have variable, use recursive function (backtrack):

 inp_sets = ([a,b],[c,d],[e,f,g])  max_col = len(inp_sets)  def generate(col_index, current_set):      if col_index == max_col:          if test(current_set):              return current_set          else:              return none      else:          found = false          item in inp_sets[col_index]:              res = generate(col_index+1, current_set + [item]):              if res:                   return res              elif (col_index == max_col - 1):                   # here skipping rest of checks last column                   # change condition if want skip more columns                   return none  result = generate(0, []) 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -