Генерация всех перестановок данного списка.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
hd = lambda list: list[0]
rest = lambda list: list[1:]
def insert(list, elm):
first_len = len(hd(list))
new_list = [[x[:] for i in xrange(first_len + 1)] for x in list]
for i in new_list:
f = 0
for j in i:
j.insert(f, elm)
f += 1
return reduce(lambda acc, x: acc + x, new_list, [])
def perm(list):
if len(list) == 0:
[]
if len(list) == 1:
return [list]
for i in list:
return insert(perm(rest(list)), hd(list))