[Tutor] combinatorics problem: assembling array
Kent Johnson
kent37 at tds.net
Thu Jan 21 15:14:51 CET 2010
On Thu, Jan 21, 2010 at 8:07 AM, Kent Johnson <kent37 at tds.net> wrote:
> On Thu, Jan 21, 2010 at 4:06 AM, markus kossner <m.kossner at tu-bs.de> wrote:
>> Dear Pythonics,
>> I have a rather algorithmic problem that obviously made a knot in my brain:
>>
>> Assume we have to build up all the arrays that are possible if we have a
>> nested array
>> containing an array of integers that are allowed for each single position.
>> For example
>> the nested array
>> (
>> (1,2,3,89),
>> (3,5,8),
>> (19,30,7,100,210,1,44)
>> )
>> would define all the arrays of length 3 that can be enumerated using the
>> array of possible numbers for each position.
>
> See itertools.product():
> http://docs.python.org/library/itertools.html#itertools.product
Here is an example:
In [1]: values = (
...: (1,2,3,89),
...: (3,5,8),
...: (19,30,7,100,210,1,44)
...: )
In [2]: from itertools import product
In [3]: list(product(*values))
Out[3]:
[(1, 3, 19),
(1, 3, 30),
(1, 3, 7),
...
(2, 3, 19),
(2, 3, 30),
...
(89, 8, 210),
(89, 8, 1),
(89, 8, 44)]
Kent
More information about the Tutor
mailing list