Easy List question

M

Matt Williams

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

Russell Blau

Matt Williams said:
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)

When I run this script, I get the following output:

['a', 'b', 'c']
['a', 'c', 'b']
['b', 'c', 'a']
['b', 'a', 'c']
['c', 'a', 'b']
['c', 'b', 'a']
print l ['a', 'b', 'c']
print s
[['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a',
'b', 'c'], ['a', 'b', 'c']]

What were you expecting?

Here are a few clues:

When you call permute(l), "l" is a global variable that points to the list
["a", "b", "c"]. "list" is a local variable in your function that points to
the *same* list ["a", "b", "c"]. In your function, you use "list" in some
places and "l" in others, but they are both operating on the same list.
This, I strongly suspect, is not what you intended.

(Incidentally, it is legal but not advisable to use the word "list" as a
variable name, since "list" is a built-in identifier in Python, and using it
as a variable name overrides the built-in usage.)
 
A

Abe Mathews

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
sparada

Latest Threads

Top