List Combinations

Mark Dickinson dickinsm at gmail.com
Wed Mar 12 11:25:41 EDT 2008


On Mar 12, 10:18 am, Gerdus van Zyl <gerdusvan... at gmail.com> wrote:
> I have a list that looks like this:
> [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]
>
> how can I get all the combinations thereof that looks like as follows:

You could wait for Python 2.6, or download the current alpha:

Python 2.6a1+ (trunk:61353M, Mar 12 2008, 11:21:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import product
>>> for c in product(['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']): print c
...
('3', '9', '5', '4', '2')
('3', '9', '5', '4', '5')
('3', '9', '5', '4', '8')
('3', '1', '5', '4', '2')
('3', '1', '5', '4', '5')
('3', '1', '5', '4', '8')

If you can't wait, look at http://docs.python.org/dev/library/itertools.html
where equivalent code is given.

Mark



More information about the Python-list mailing list