[Tutor] Calculating and returning possible combinations of elements from a given set

ZUXOXUS zuxoxus at gmail.com
Wed Jul 28 18:07:25 CEST 2010


>
> 2010/7/28 Dave Angel <davea at ieee.org>
> <snip>
>
> Your latest version gets the product of two.  But if you want an arbitrary
>> number, instead of repeating the iterable ('ABC' in your case), you can
>> use
>> a repeat count.  That's presumably what you were trying to do in your
>> earlier incantation.  But you forgot the 'repeat' keyword:
>>
>> for prod in itertools.product('ABC', repeat=4):
>>  xxxx
>>
>> will give you all the four-tuples.
>>
>> DaveA
>>
>>
>>
>>
>
> <snip>
>
> Thanks for the reminder, Dave Angel, and for the 'repeat' keyword!
>
>
>
Since you're new to Python, it's probably useful to expound a little bit, on
how you could have figured it out from the help documentation.

itertools.product(/*iterables/[, /repeat/])

  Cartesian product of input iterables.

  Equivalent to nested for-loops in a generator expression. For
  example, product(A, B) returns the same as ((x,y) for x in A for y
  in B).

  The nested loops cycle like an odometer with the rightmost element
  advancing on every iteration. This pattern creates a lexicographic
  ordering so that if the input’s iterables are sorted, the product
  tuples are emitted in sorted order.

  To compute the product of an iterable with itself, specify the
  number of repetitions with the optional /repeat/ keyword argument.
  For example, product(A, repeat=4) means the same as product(A, A, A, A).

....

Now that example at the end is exactly what you need here. But let's look at
the first line.

See the *iterables in the formal parameter list. The leading * means you can
put 1, 2, or as many iterables as you like, and they'll each be treated as
an independent dimension in the cartesian product. So when you put the
arguments,
...product("ABC", 2)

the 2 is treated as an iterable, which it isn't. Thus your error. When there
are an arbitrary number of such arguments, followed by another optional
parameter, there's no way the compiler could guess in which sense you wanted
the 2 to be used. So you have to use that parameter's name as a keyword in
your call. If you have a function declared as
def product(*iterables, repeat):
.....

then you can call it with
count = 4
product(list1, list2, repeat=count)

and by specifying the 'repeat' keyword, the system knows to stop copying
your arguments into the iterables collection.

Actually, I suspect that if you specify a repeat count, you can only supply
one iterable, but I'm really talking about the language here.

DaveA

****

Wow, Thank you DaveA, that was very useful.

However, as it usually happens, answers trigger new questions.

My doubt now is whether I can change the way python show the combinations.

I mean, here's what python actually does:

>>> for prod in itertools.product('abc', repeat=3):
print(prod)

('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
[...] etc.


what if I want the combinations listed in a... well, in a list, kind of like
this:

('aaa', 'aab', aac', 'aba', 'abb', 'abc' [...]etc.)

can I do that?

I have checked how the function works (see below), perhaps I have to just
change couple of lines of the code and voilá, the result displayed as I
want... But unfortunately I'm too newbie for this, or this is too hardcore:

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)


Any ideas will be very much appreciated.

2010/7/28 Dave Angel <davea at ieee.org>

> ZUXOXUS wrote:
>
>> 2010/7/28 Dave Angel <davea at ieee.org>
>> <snip>
>>
>>  Your latest version gets the product of two.  But if you want an
>>> arbitrary
>>> number, instead of repeating the iterable ('ABC' in your case), you can
>>> use
>>> a repeat count.  That's presumably what you were trying to do in your
>>> earlier incantation.  But you forgot the 'repeat' keyword:
>>>
>>> for prod in itertools.product('ABC', repeat=4):
>>>  xxxx
>>>
>>> will give you all the four-tuples.
>>>
>>> DaveA
>>>
>>>
>>>
>>>
>>
>> <snip>
>>
>> Thanks for the reminder, Dave Angel, and for the 'repeat' keyword!
>>
>>
>>
> Since you're new to Python, it's probably useful to expound a little bit,
> on how you could have figured it out from the help documentation.
>
> itertools.product(/*iterables/[, /repeat/])
>
>   Cartesian product of input iterables.
>
>   Equivalent to nested for-loops in a generator expression. For
>   example, product(A, B) returns the same as ((x,y) for x in A for y
>   in B).
>
>   The nested loops cycle like an odometer with the rightmost element
>   advancing on every iteration. This pattern creates a lexicographic
>   ordering so that if the input’s iterables are sorted, the product
>   tuples are emitted in sorted order.
>
>   To compute the product of an iterable with itself, specify the
>   number of repetitions with the optional /repeat/ keyword argument.
>   For example, product(A, repeat=4) means the same as product(A, A, A, A).
>
> ....
>
> Now that example at the end is exactly what you need here. But let's look
> at the first line.
>
> See the *iterables in the formal parameter list. The leading * means you
> can put 1, 2, or as many iterables as you like, and they'll each be treated
> as an independent dimension in the cartesian product. So when you put the
> arguments,
> ...product("ABC", 2)
>
> the 2 is treated as an iterable, which it isn't. Thus your error. When
> there are an arbitrary number of such arguments, followed by another
> optional parameter, there's no way the compiler could guess in which sense
> you wanted the 2 to be used. So you have to use that parameter's name as a
> keyword in your call. If you have a function declared as
> def product(*iterables, repeat):
> .....
>
> then you can call it with
> count = 4
> product(list1, list2, repeat=count)
>
> and by specifying the 'repeat' keyword, the system knows to stop copying
> your arguments into the iterables collection.
>
> Actually, I suspect that if you specify a repeat count, you can only supply
> one iterable, but I'm really talking about the language here.
>
> DaveA
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100728/0c2ed2a9/attachment-0001.html>


More information about the Tutor mailing list