python - Contracting elements from two different lists -
i have 2 different lists list1 = ['a','b'] , list2 = ['c','d','e']. able find possible contractions between elements of 2 lists. nowadays case have code (preferably python, mathematica or matlab) takes lists above , returns:
ac,bd , ac,be , ad,bc , ad,be , ae,bc , ae,bd
which possible contractions. able lists of variable size (but 2 of them). i've played lot python's itertools can't hang of how works 2 lists. help much appreciated.
here version:
import itertools l1 = 'ab' l2 = 'cde' n = min(len(l1),len(l2)) print('; '.join( ','.join(a+b a,b in zip(s1,s2)) s1,s2 in itertools.product( itertools.permutations(l1,n), itertools.combinations(l2,n), ) )) this output:
ac,bd; ac,be; ad,be; bc,ad; bc,ae; bd,ae note shortness, did not build list of items, straight iterated strings. not matter of 2 lists gets permutations , gets combinations, changes order of output. permutations takes possible orderings, while combinations returns sorted orderings. way, each contraction once.
for each contraction, 2 sequences s1 , s2, contraction between elements of index in each sequence. ','.join(a+b a,b in zip(s1,s2)) makes nice string such contraction.
python matlab wolfram-mathematica
No comments:
Post a Comment