Python Permutation with Limits and Directionality -


so have problem can't head around, can give pseudocode @ best.

lista=(a,b,c) listb=(a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3) 

i need way limit results of permutations of listsb following criteria:

a tuple of items contained in lista

the order of lista needs retained

the permutations can right

lista , list b can length

for example:

acceptable:

a1,b1,c1

a2,b3,c3

unacceptable:

a1,b1,d1

a2,b1,c3

b2,a2,c3

any ideas have appreciated! thanks

i'm not sure calling "permutations", since seems want one-of-each combination.

probably straightforward write own recursive generator. used string operations , startswith determine association between lista , listb; can want if have different objects.

lista=('a','b','c') listb=('a1','b1','c1','d1','a2','b2','c2','d2','a3','b3','c3','d3')  def gen_combos(li, keepers, builder=tuple()):     if not keepers:         yield builder     else:         i,x in enumerate(li):             if x.startswith(keepers[0]):                 combo in gen_combos(li[i+1:], keepers[1:], builder + (x,)):                     yield combo 

demo:

list(gen_combos(listb, lista)) out[40]:  [('a1', 'b1', 'c1'),  ('a1', 'b1', 'c2'),  ('a1', 'b1', 'c3'),  ('a1', 'b2', 'c2'),  ('a1', 'b2', 'c3'),  ('a1', 'b3', 'c3'),  ('a2', 'b2', 'c2'),  ('a2', 'b2', 'c3'),  ('a2', 'b3', 'c3'),  ('a3', 'b3', 'c3')] 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -