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

2010-07-29 Thread ZUXOXUS
2010/7/28 Dave Angel da...@ieee.org

 ZUXOXUS wrote:

 snip

 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.

 snip, double-included history

 Well itertools.product() already returns an iterator that's equivalent to a
 list of tuples.   You can print that list simply by doing something like:
 print list(itertools.product('abc', repeat=3))

 So your question is how you can transform such a list into a list of
 strings instead.

 so try each of the following.


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

 print [.join(prod) for prod in itertools.product('abc', repeat=3)]

 DaveA


***

Hi DaveA, the second option returns exactly the result I wanted:

 print([.join(prod) for prod in itertools.product('abc', repeat=3)])
['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa',
'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab', 'cac',
'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

Thank you very much.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-28 Thread ZUXOXUS
2010/7/28 Dave Angel da...@ieee.org



 ZUXOXUS wrote:

 Oh, I think i got it:



 for prod in itertools.product('ABC', 'ABC'):


 print(prod)

 ('A', 'A')
 ('A', 'B')
 ('A', 'C')
 ('B', 'A')
 ('B', 'B')
 ('B', 'C')
 ('C', 'A')
 ('C', 'B')
 ('C', 'C')

 Thank you very much!!

 2010/7/28 ZUXOXUS zuxo...@gmail.com


 You're top-posting, which loses all the context. In this forum, put your
 comments after whatever lines you're quoting.

 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):
   

 will give you all the four-tuples.

 DaveA




 Hey


Sorry for the top-posting, I forgot this rule

Thanks for the reminder, Dave Angel, and for the 'repeat' keyword!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-28 Thread Dave Angel

ZUXOXUS wrote:

2010/7/28 Dave Angel da...@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):
  

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




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-28 Thread ZUXOXUS

 2010/7/28 Dave Angel da...@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):
  

 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 da...@ieee.org

 ZUXOXUS wrote:

 2010/7/28 Dave Angel da...@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):
  

 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, 

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

2010-07-28 Thread Dave Angel

ZUXOXUS wrote:

snip
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.

snip, double-included history
Well itertools.product() already returns an iterator that's equivalent 
to a list of tuples.   You can print that list simply by doing something 
like:

print list(itertools.product('abc', repeat=3))

So your question is how you can transform such a list into a list of 
strings instead.


so try each of the following.

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

print [.join(prod) for prod in itertools.product('abc', repeat=3)]

DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread Nick Raptis

On 07/28/2010 01:20 AM, ZUXOXUS wrote:

Hi all pythoners

I've got a probably easy to answer question.

Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 
'ultra'.


They are in a list, or in a sequence or whatever, say a bag of words

And now I want to know how many couples I can do with them, and I want 
the program to show me the actual couples: 'manman', 'manbat', 
'mansuper', 'manultra', 'batbat', 'batman', 'batsuper', etc.


But hey, why building up new words from just two strings? I also want 
to know the possible combinations of three words, four words, and 
perhaps, why not, five words.


So, is it easy to do?

Sorry, I'm new in programing, and am probably far from being a math-master

I'm clueless, I think probably the code have some FOR I IN SEQUENCE... 
but then what? I don't know how to say: take every element and paste 
it to another one from the bag, and with another one, and with another 
one,...


If it's too complex, I dont need the whole code recipe, just need some 
clues, or perhaps a useful link


Thank you very much in advance!



Take a look in the itertools module 
http://docs.python.org/library/itertools.html
Check the section *Combinatoric generators: (website doesn't have an 
anchor link for that, search around a bit)


Nick
*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread Mark Lawrence

On 27/07/2010 23:20, ZUXOXUS wrote:

Hi all pythoners

I've got a probably easy to answer question.

Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'.

They are in a list, or in a sequence or whatever, say a bag of words

And now I want to know how many couples I can do with them, and I want the
program to show me the actual couples: 'manman', 'manbat', 'mansuper',
'manultra', 'batbat', 'batman', 'batsuper', etc.

But hey, why building up new words from just two strings? I also want to
know the possible combinations of three words, four words, and perhaps, why
not, five words.

So, is it easy to do?

Sorry, I'm new in programing, and am probably far from being a math-master

I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
then what? I don't know how to say: take every element and paste it to
another one from the bag, and with another one, and with another one,...

If it's too complex, I dont need the whole code recipe, just need some
clues, or perhaps a useful link

Thank you very much in advance!

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


The lazy way.
http://docs.python.org/library/itertools.html
Look for combinations().

HTH.

Mark Lawrence.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread Mac Ryan
On Tue, 2010-07-27 at 23:31 +0100, Mark Lawrence wrote:
 On 27/07/2010 23:20, ZUXOXUS wrote:
  Hi all pythoners
 
  I've got a probably easy to answer question.
 
  Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'.
 
  They are in a list, or in a sequence or whatever, say a bag of words
 
  And now I want to know how many couples I can do with them, and I want the
  program to show me the actual couples: 'manman', 'manbat', 'mansuper',
  'manultra', 'batbat', 'batman', 'batsuper', etc.
 
  But hey, why building up new words from just two strings? I also want to
  know the possible combinations of three words, four words, and perhaps, why
  not, five words.
 
  So, is it easy to do?
 
  Sorry, I'm new in programing, and am probably far from being a math-master
 
  I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
  then what? I don't know how to say: take every element and paste it to
  another one from the bag, and with another one, and with another one,...
 
  If it's too complex, I dont need the whole code recipe, just need some
  clues, or perhaps a useful link
 
  Thank you very much in advance!
 
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
 The lazy way.
 http://docs.python.org/library/itertools.html
 Look for combinations().

From the examples listed by the OP (see: manbat, batman) I actually
believe he is looking for the cartesian product.

The syntax should therefore be (DISCLAIMER: I am going by memory without
having the possibility of testing right now):

import itertools
for prod in itertools.product(my_list_of_words, 2):
print prod

(The two as second parameter tells that you are wishing to have a
bi-dimensional product, with both axis having the same list of words.
See docs for more info)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread ZUXOXUS
Sharp thanks, but:

I try to reproduce the example from the table, but:

 import itertools
 combinations('ABC', 2)
Traceback (most recent call last):
  File pyshell#27, line 1, in module
combinations('ABC', 2)
NameError: name 'combinations' is not defined


If im not mistaken, it should return AB, AC, BA, etc.

I'm using Python 3.1


2010/7/28 Mark Lawrence breamore...@yahoo.co.uk

 On 27/07/2010 23:20, ZUXOXUS wrote:

 Hi all pythoners

 I've got a probably easy to answer question.

 Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
 'ultra'.

 They are in a list, or in a sequence or whatever, say a bag of words

 And now I want to know how many couples I can do with them, and I want the
 program to show me the actual couples: 'manman', 'manbat', 'mansuper',
 'manultra', 'batbat', 'batman', 'batsuper', etc.

 But hey, why building up new words from just two strings? I also want to
 know the possible combinations of three words, four words, and perhaps,
 why
 not, five words.

 So, is it easy to do?

 Sorry, I'm new in programing, and am probably far from being a math-master

 I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
 then what? I don't know how to say: take every element and paste it to
 another one from the bag, and with another one, and with another one,...

 If it's too complex, I dont need the whole code recipe, just need some
 clues, or perhaps a useful link

 Thank you very much in advance!

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


 The lazy way.

 http://docs.python.org/library/itertools.html
 Look for combinations().

 HTH.

 Mark Lawrence.



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread ZUXOXUS
Mac,

this is what I get:

 for prod in itertools.product('ABC', 2):
print(prod)

Traceback (most recent call last):
  File pyshell#34, line 1, in module
for prod in itertools.product('ABC', 2):
TypeError: 'int' object is not iterable


hmm, what might be that 'int' object? 2?


2010/7/28 ZUXOXUS zuxo...@gmail.com

 Sharp thanks, but:

 I try to reproduce the example from the table, but:

  import itertools
  combinations('ABC', 2)
 Traceback (most recent call last):
   File pyshell#27, line 1, in module
 combinations('ABC', 2)
 NameError: name 'combinations' is not defined
 

 If im not mistaken, it should return AB, AC, BA, etc.

 I'm using Python 3.1


 2010/7/28 Mark Lawrence breamore...@yahoo.co.uk

 On 27/07/2010 23:20, ZUXOXUS wrote:

 Hi all pythoners

 I've got a probably easy to answer question.

 Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
 'ultra'.

 They are in a list, or in a sequence or whatever, say a bag of words

 And now I want to know how many couples I can do with them, and I want
 the
 program to show me the actual couples: 'manman', 'manbat', 'mansuper',
 'manultra', 'batbat', 'batman', 'batsuper', etc.

 But hey, why building up new words from just two strings? I also want to
 know the possible combinations of three words, four words, and perhaps,
 why
 not, five words.

 So, is it easy to do?

 Sorry, I'm new in programing, and am probably far from being a
 math-master

 I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
 but
 then what? I don't know how to say: take every element and paste it to
 another one from the bag, and with another one, and with another one,...

 If it's too complex, I dont need the whole code recipe, just need some
 clues, or perhaps a useful link

 Thank you very much in advance!

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


 The lazy way.

 http://docs.python.org/library/itertools.html
 Look for combinations().

 HTH.

 Mark Lawrence.



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread ZUXOXUS
Oh, I think i got it:

 for prod in itertools.product('ABC', 'ABC'):
print(prod)

('A', 'A')
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'B')
('B', 'C')
('C', 'A')
('C', 'B')
('C', 'C')

Thank you very much!!

2010/7/28 ZUXOXUS zuxo...@gmail.com

 Mac,

 this is what I get:

  for prod in itertools.product('ABC', 2):
 print(prod)

 Traceback (most recent call last):
   File pyshell#34, line 1, in module
 for prod in itertools.product('ABC', 2):
 TypeError: 'int' object is not iterable


 hmm, what might be that 'int' object? 2?


 2010/7/28 ZUXOXUS zuxo...@gmail.com

 Sharp thanks, but:

 I try to reproduce the example from the table, but:

  import itertools
  combinations('ABC', 2)
 Traceback (most recent call last):
   File pyshell#27, line 1, in module
 combinations('ABC', 2)
 NameError: name 'combinations' is not defined
 

 If im not mistaken, it should return AB, AC, BA, etc.

 I'm using Python 3.1


 2010/7/28 Mark Lawrence breamore...@yahoo.co.uk

 On 27/07/2010 23:20, ZUXOXUS wrote:

 Hi all pythoners

 I've got a probably easy to answer question.

 Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
 'ultra'.

 They are in a list, or in a sequence or whatever, say a bag of words

 And now I want to know how many couples I can do with them, and I want
 the
 program to show me the actual couples: 'manman', 'manbat', 'mansuper',
 'manultra', 'batbat', 'batman', 'batsuper', etc.

 But hey, why building up new words from just two strings? I also want to
 know the possible combinations of three words, four words, and perhaps,
 why
 not, five words.

 So, is it easy to do?

 Sorry, I'm new in programing, and am probably far from being a
 math-master

 I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
 but
 then what? I don't know how to say: take every element and paste it to
 another one from the bag, and with another one, and with another one,...

 If it's too complex, I dont need the whole code recipe, just need some
 clues, or perhaps a useful link

 Thank you very much in advance!

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


 The lazy way.

 http://docs.python.org/library/itertools.html
 Look for combinations().

 HTH.

 Mark Lawrence.



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-07-27 Thread Dave Angel



ZUXOXUS wrote:

Oh, I think i got it:

  

for prod in itertools.product('ABC', 'ABC'):


print(prod)

('A', 'A')
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'B')
('B', 'C')
('C', 'A')
('C', 'B')
('C', 'C')

Thank you very much!!

2010/7/28 ZUXOXUS zuxo...@gmail.com
  

You're top-posting, which loses all the context. In this forum, put your 
comments after whatever lines you're quoting.


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):
   

will give you all the four-tuples.

DaveA




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor