recursive list comprehension

S

SimonVC

Is there a way to do this as a list comprehension?
.... if len(alist)==0: print blist
.... for i in range(len(alist)):
.... blist.append(alist.pop(i))
.... recu(alist, blist)
.... alist.insert(i, blist.pop())
['a', 'b', 'c']
['a', 'c', 'b']
['b', 'a', 'c']
['b', 'c', 'a']
['c', 'a', 'b']
['c', 'b', 'a']


Cheers
SimonVC

keywords: python recursive permutations algorithm combination jumble
 
P

Paul McGuire

SimonVC said:
Is there a way to do this as a list comprehension?
def recu(alist, blist=[]):
... if len(alist)==0: print blist
... for i in range(len(alist)):
... blist.append(alist.pop(i))
... recu(alist, blist)
... alist.insert(i, blist.pop())
['a', 'b', 'c']
['a', 'c', 'b']
['b', 'a', 'c']
['b', 'c', 'a']
['c', 'a', 'b']
['c', 'b', 'a']


Cheers
SimonVC

keywords: python recursive permutations algorithm combination jumble

Perhaps this cookbook recipe would be of help to you:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297

It describes how to access the current list comp from within the list comp
itself.

-- Paul
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top