[Tutor] permutations?

Luke Paireepinart rabidpoobear at gmail.com
Thu Dec 2 15:07:06 CET 2010


Instead of nested fors which will grow exponentially in running time, consider this for removing reversals:
newlist = []
tempdict = {}
for i in oldlist:
    try:
        tempdict[i]
    except:
        tempdict[i] = 1
        tempdict[i.reverse()] = 1
        newlist.append(i)


That's the gneral idea. Pseudocode, so untested. You exploit the O(1) hash lookup of the dict to avoid the runtime cost of the nested loops, and it is easier to read as well. Would need to timeit to be sure it helps with your data sets though. They may be small enough that it doesn't matter.

-----------------------------
Sent from a mobile device with a bad e-mail client.
-----------------------------

On Dec 2, 2010, at 7:54 AM, शंतनू <shantanoo at gmail.com> wrote:

> Following link could be useful:
> 
> http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python
> 
> On Thu, Dec 2, 2010 at 04:15, Alex Hall <mehgcap at gmail.com> 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.
> 
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehgcap at gmail.com; http://www.facebook.com/mehgcap
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101202/05cfe42d/attachment-0001.html>


More information about the Tutor mailing list