Re: Easy List question

From: Abe Mathews (59Bassman_at_gmail.com)
Date: 10/05/04


Date: Tue, 5 Oct 2004 11:03:26 -0400
To: Matt Williams <matt@mwilliams.org>

I apologize in advance as I'm new to python, and relatively new to
programming. However, I took this as a bit of a challenge this
morning to see if I could come up with another method for generating
the permutations of a list.

This is longer than what you proposed, but it makes sense to me and it
doesn't use the "list" name which I believe is reserved in Python.
Permute should return a list containing all of the permutations of the
list that you have given it.

Abe Mathews
------------------------------------------

def permute(orig_list):
    newlist = [] # this will be a list of permutated lists
    modlist = orig_list[:] # make a copy so we don't mess with the original
    len_l = len(orig_list)

    for i in range(0,len_l):
        if len_l==1:
            return [orig_list]
        else:
            list_tail = permute(modlist[1:])
            for c in list_tail:
                c.insert(0,modlist[0])
        newlist.extend(list_tail)
        new_end = modlist.pop(0)
        modlist.extend(new_end)
 
    return newlist

l = ["a","b","c","d"]
s = permute(l)

for i in s:print i
print len(s)

On 5 Oct 2004 04:39:49 -0700, Matt Williams <matt@mwilliams.org> wrote:
> Dear list,
>
> Can anyone explain why the append method here returns an 'unpermuted'
> list (and how to solve it_?
>
> I'm stuck?
>
> s=[]
> def permute(list):
> len_l = len(list)
> if len_l == 1:
> print l
> s.append(l)
> for i in range (0, len_l):
> permute(list[1:])
> # now change position of first element with next (rotate will
> do it?)
> list.append(list.pop(0))
> # reflect this change in the main list
> l[len(l) - len_l:] = list
>
> l=["a","b","c"]
>
> permute(l)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



Relevant Pages

  • Re: Generating all permutations from a regexp
    ... With some help from the sre_parse module (and the Python Cookbook), ... import string ... def unique_extend: ... Returns a list of strings of possible permutations for this regexp ...
    (comp.lang.python)
  • text adventure question
    ... I am working on a text adventure game for python to get back into ... def character_sheet: ... global reputation ... print "Please choose another command. ...
    (comp.lang.python)
  • Re: noob question: "TypeError" wrong number of args
    ... or it's newer syntactic-sugar-version would become somewhat more ... Python source code and outputs the same source code with only one change: ... insert the string 'self" as the first parameter of every "def ... Python code, which is unavoidable for such a syntactic change. ...
    (comp.lang.python)
  • Re: Python 3K or Python 2.9?
    ... function/method argument that might as well be hidden in the compiler ... You could add some syntax to Python such that '.x' was equivalent to ... def factory: ... C.method = foo ...
    (comp.lang.python)
  • Crude statistics on the standard library
    ... def browse_stdlib: ... # remove some obsolete modules ('this' + DeprecationWarning) ... Parts of output of python -i statsmod.py ... referrers of __future__ ...
    (comp.lang.python)