Combinations of lists
Alain Ketterlin
alain at dpt-info.u-strasbg.fr
Wed Oct 3 11:11:42 EDT 2012
Steen Lysgaard <boxeakasteen at gmail.com> writes:
> I am looking for a clever way to compute all combinations of two
> lists. Look at this example:
>
> h = ['A','A','B','B']
> m = ['a','b']
>
> 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:
>
> [['aA', 'aA', 'bB', 'bB'],
> ['aA', 'aB', 'bA', 'bB'],
> ['aB', 'aB', 'bA', 'bA']]
I can't make sense of your explanation, which doesn't seem to match your
example (the result is not of the same size as h).
Here is a way to compute { xh | x in m }, where xh is a list where x is
prepended to each element of h.
result = [ [ x+l for l in h ] for x in m ]
If there is no duplicate in the original lists, then there will be no
duplicate in the result. Is that what you are looking for?
-- Alain.
More information about the Python-list
mailing list