![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
List permutations?
How would you go about doing list permutations in python? I've seen permutations in C++, C, and Java. but I wanna see what it looks like in python
![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
By list permutations I assume you mean a function that returns all possible permutations of a list?
I'd use a recursive generator for a task like that: def permutations(x):
if len(x) <= 1:
yield x
else:
for i in range(len(x)):
for p in permutations(x[:i] + x[i + 1:]):
yield [x[i]] + p |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|