[Tutor] permutations?

Alex Hall mehgcap at gmail.com
Thu Dec 2 04:55:10 CET 2010


Alright, I have it working. Now the problem is that it does not throw
out reversals. I tried to do this myself with a couple loops, but I
get index errors. My total list of permutations is called l.

for i in range(0, len(l)):
 r=l[i]; r.reverse()
 for j in range(0, len(l)):
  print l[j], r, i, j

  if r==l[j]: l.remove(r)


  if r==l[j]: l.remove(r)
IndexError: list index out of range

When it has found two repeats (oddly, they are the same repeat - 2,1)
and removed them, there are ten of the twelve items left. Once j hits
10, it errors out, obviously, since it is looking for l[10] which no
longer exists. However, should the loop not re-evaluate len(l) each
time, so this should not be a problem? Why is it still looking for an
index that is no longer in the range (0, len(l)), and how can I fix
this? Also, why would it find several items twice? For example, it
picks up (2,1), then picks it up again.
Is there a better way of doing this that would avoid me having to
write the function?

On 12/1/10, Alex Hall <mehgcap at gmail.com> wrote:
> Thanks to everyone for the itertools hint; that sounds like it will work.
>
> Sorry I was not clearer:
> 1. Order matters; I meant to say that direction does not. That is, 123
> is not the same as 213, but 123 is the same as 321 since the second
> example is simply a reversal.
> 2. I am looking for all permutations and subpermutations (if that is a
> word) of 1-n where the list must have at least 2, but no more than n,
> unique numbers (so 1 is not valid, nor is 1231 since it repeats 1 and
> is too long for n=3).
> I hope that makes sense. However, hopefully itertools will do it; if I
> run into problems I will respond to this email to keep it in the same
> thread. Thanks again! Oh, to the person who asked, I have 2.6 and 2.7
> installed, with the default being 2.6.
>
> On 12/1/10, bob gailer <bgailer at gmail.com> wrote:
>> On 12/1/2010 5:45 PM, Alex Hall wrote:
>>> Hi all,
>>> I am wondering if there is a python package that will find
>>> permutations? For example, if I have (1, 2, 3), the possibilities I
>>> want are:
>>> 12
>>> 13
>>> 23
>>> 123
>>> 132
>>> 231
>>>
>>> Order does not matter; 21 is the same as 12, but no numbers can
>>> repeat. If no package exists, does someone have a hint as to how to
>>> get a function to do this? The one I have right now will not find 132
>>> or 231, nor will it find 13. TIA.
>>
>> According to Wikipedia " there are six permutations of the set {1,2,3},
>> namely [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]."
>>
>> Above you show some "combinations" and a subset of the permutations.
>>
>> What rules did you apply to come up with your result?
>>
>> --
>> Bob Gailer
>> 919-636-4239
>> Chapel Hill NC
>>
>>
>
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehgcap at gmail.com; http://www.facebook.com/mehgcap
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list