All permutations of a list
June Kim
junaftnoon at nospamplzyahoo.com
Mon Oct 23 02:23:08 EDT 2000
More information about the Python-list mailing list
Mon Oct 23 02:23:08 EDT 2000
- Previous message (by thread): All permutations of a list
- Next message (by thread): All combinations of a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Rainer Deyke" <root at rainerdeyke.com> wrote in message news:ycQI5.111500$g6.50529775 at news2.rdc2.tx.home.com... > "Emile van Sebille" <emile at fenx.com> wrote in message > news:8t08bb$lnvu5$1 at ID-11957.news.cis.dfn.de... > > "Rainer Deyke" <root at rainerdeyke.com> wrote in message > > news:9nNI5.110763$g6.50159527 at news2.rdc2.tx.home.com... > > > Here's my short Pythonic permutation function: > > > > > > def perms(list): > > > if list == []: > > > return [[]] > > > return [[list[i]] + p for i in range(len(list))\ > > > for p in perms(list[:i] + list[i+1:])] > > > > > > > ... which permutes into... > > > > def perms3(list): > > return not [list] or [[list[i]] + p for i in > > range(len(list))\ > > for p in perms(list[:i] + list[i+1:])] > > No. If 'list' is '[]', '[list]' evalutes to '[[]]'. 'not [[]]' evalutes to > 0, effectively removing all special handling for the empty list. > > If the list of permutations of the empty list was redefined as the empty > list, the following would work: > > def perms(list): > return list and [[list[i]] + p for i in range(len(list))\ > for p in (perms(list[:i] + list[i+1:]) or [[]])] > > If you're really desparate, you can save an additional line by using lambda: > > perms = lambda list: list and [[list[i]] + p for i in\ > range(len(list)) for p in (perms(list[:i] + list[i+1:]) or [[]])] > > Cool! Those are all braintwisters to me. I think I'd feel more clever after studying some of you guys' snippets. It seems, the more I try to understand the codes, the more I get to respect you. Surely this Python thing must go for the Programming for Everyone even in elementary schools, which will make our students squeeze their brains and let them be more pythonically clever. > -- > Rainer Deyke (root at rainerdeyke.com) > Shareware computer games - http://rainerdeyke.com > "In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor > > Best Regards, June
- Previous message (by thread): All permutations of a list
- Next message (by thread): All combinations of a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list