Re: Question about permutations (itertools)

2010-05-31 Thread Xavier Ho
  list(combinations_with_replacement('01',3))
 ('0', '0', '0')
 ('0', '0', '1')
 ('0', '1', '1')
 ('1', '1', '1')

 Is it possible to get combinations_with_replacement to return numbers
 rather than strings? (see above)


 list(combinations_with_replacement(range(0,2), 3))
[(0, 0, 0), (0, 0, 1), (0, 1, 1), (1, 1, 1)]


Hopefully that'll give you some ideas.

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about permutations (itertools)

2010-05-31 Thread Peter Otten
Vincent Davis wrote:

 As a note I am doing this in py3.
 
 I am looking for the most efficient (speed) way to produce an an
 iterator to of permutations.
 One of the problem I am having it that neither combinations nor
 permutations does not exactly what I want directly.
 For example If I want all possible ordered lists of 0,1 of length 3
 (0,0,0)
 (0,0,1)
 (0,1,1)
 (1,1,1)
 (1,0,1)
 (1,1,0)
 (1,0,0)
 I don't see a way to get this directly from the itertools. But maybe I
 am missing something.

 for t in itertools.product([0, 1], repeat=3):
... print(t)
...
(0, 0, 0)
(0, 0, 1)
(0, 1, 0) # Seems you missed one
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

 I see ways to get a bigger list and then remove
 duplicates.
 
 list(permutations([0,1], 3))
 []
 
 list(combinations_with_replacement('01',3))
 ('0', '0', '0')
 ('0', '0', '1')
 ('0', '1', '1')
 ('1', '1', '1')
 
 Is it possible to get combinations_with_replacement to return numbers
 rather than strings? (see above)

 for t in itertools.combinations_with_replacement([0, 1], 3):
... print(t)
...
(0, 0, 0)
(0, 0, 1)
(0, 1, 1)
(1, 1, 1)

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about permutations (itertools)

2010-05-31 Thread Vincent Davis
On Mon, May 31, 2010 at 8:17 AM, Xavier Ho cont...@xavierho.com wrote:

  list(combinations_with_replacement('01',3))
 ('0', '0', '0')
 ('0', '0', '1')
 ('0', '1', '1')
 ('1', '1', '1')

 Is it possible to get combinations_with_replacement to return numbers
 rather than strings? (see above)

 list(combinations_with_replacement(range(0,2), 3))
 [(0, 0, 0), (0, 0, 1), (0, 1, 1), (1, 1, 1)]

Thanks, for some reason I didn't combinations_with_replacement took a
list as an argument.

 Hopefully that'll give you some ideas.

 Cheers,
 Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about permutations (itertools)

2010-05-31 Thread Mark Dickinson
On May 31, 3:04 pm, Vincent Davis vinc...@vincentdavis.net wrote:
 For example If I want all possible ordered lists of 0,1 of length 3
 (0,0,0)
 (0,0,1)
 (0,1,1)
 (1,1,1)
 (1,0,1)
 (1,1,0)
 (1,0,0)
 I don't see a way to get this directly from the itertools. But maybe I
 am missing something.

In this case, you're missing itertools.product:

 list(itertools.product([0, 1], repeat=3))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1,
1, 0), (1, 1, 1)]

--
Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about permutations (itertools)

2010-05-31 Thread Ulrich Eckhardt
Vincent Davis wrote:
 I am looking for the most efficient (speed) way to produce an an
 iterator to of permutations.
 One of the problem I am having it that neither combinations nor
 permutations does not exactly what I want directly.
 For example If I want all possible ordered lists of 0,1 of length 3
 (0,0,0)
 (0,0,1)
 (0,1,1)
 (1,1,1)
 (1,0,1)
 (1,1,0)
 (1,0,0)
 I don't see a way to get this directly from the itertools. But maybe I
 am missing something. I see ways to get a bigger list and then remove
 duplicates.

You have three digits where each digit can have two values (binary digits,
a.k.a. bits), so the number of combinations is 2*2*2 = 8. Even if the
possible values where unevenly distributed, you could calculate the number
of combinations by multiplying. Then, there are two different approaches:
1. count with an integer and then dissect into digits
# Note: Using // for integer division in Python3!
digit0 = n % base0
digit1 = (n // base0) % base1
digit2 = (n // base0 // base1) % base2

2. simulate digits and detect overflow
Here you simply count up the ones and if they overflow, you reset them to
zero and count up the tens.


What I don't really understand is what you mean with ordered lists.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

-- 
http://mail.python.org/mailman/listinfo/python-list