470,855 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

recursive list comprehension

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())
recu(list("abc"))

['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
Jul 18 '05 #1
1 3735
"SimonVC" <py****@simon.vc> wrote in message
news:fa**************************@posting.google.c om...
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())
recu(list("abc"))

['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/Coo.../Recipe/204297

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

-- Paul
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
35 posts views Thread by Moosebumps | last post: by
10 posts views Thread by Steve Goldman | last post: by
21 posts views Thread by Timothy Babytch | last post: by
7 posts views Thread by Jon Slaughter | last post: by
6 posts views Thread by jena | last post: by
18 posts views Thread by a | last post: by
4 posts views Thread by Gregory Guthrie | last post: by
7 posts views Thread by cesco | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.