<div>>>> t1 = [1,2,3]</div><div>>>> t2 = [4,5,6]</div><div>>>> [(x,y) for x in t1 for y in t2]</div><div>[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]</div><div><br></div>
<br><div class="gmail_quote">2012/10/3 Steen Lysgaard <span dir="ltr"><<a href="mailto:boxeakasteen@gmail.com" target="_blank">boxeakasteen@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I am looking for a clever way to compute all combinations of two lists. Look at this example:<br>
<br>
h = ['A','A','B','B']<br>
m = ['a','b']<br>
<br>
the resulting combinations should be of the same length as h and each element in m can be used twice. The sought after result using h and m from above is:<br>
<br>
[['aA', 'aA', 'bB', 'bB'],<br>
 ['aA', 'aB', 'bA', 'bB'],<br>
 ['aB', 'aB', 'bA', 'bA']]<br>
<br>
(the order of the results does not matter i.e. ['aA', 'aA', 'bB', 'bB'] and ['aA', 'bB', 'aA', 'bB'] are considered the same)<br>
<br>
This is achieved by the code below, this however needs to go through all possible combinations (faculty of len(h)) and rule out duplicates as they occur and this is too much if for example len(h) is 16.<br>
<br>
Can anyone guide me to a better solution?<br>
<br>
Thanks,<br>
Steen<br>
<br>
h = ['A','A','B','B']<br>
m = ['a','b']<br>
<br>
c = []<br>
for i in h:<br>
    c.append([])<br>
    for j in m:<br>
        c[-1].append(j+i)<br>
        c[-1].append(j+i)<br>
<br>
combs = []<br>
<br>
for a in permutations(range(len(h)),<u></u>len(h)):<br>
    comb = []<br>
    for i in range(len(h)):<br>
        comb.append(c[i][a[i]])<br>
    comb.sort()<br>
<br>
    if comb not in combs:<br>
        combs.append(comb)<br>
<br>
print combs<br>
<br>
print len(combs)<span class="HOEnZb"><font color="#888888"><br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>--</div><div>ICQ:#7499099</div><div><a href="mailto:JID%3Asoider@jabber.ru" target="_blank">JID:soider@jabber.ru</a></div><div><br></div><br>