您应该阅读 my blog post 关于这种排列(以及其他内容)以获得更多背景信息 - 并点击那里的一些链接。
这是我的词典排列生成器的一个版本,它按照 Steinhaus-Johnson-Trotter 排列生成器的生成序列按要求完成:
def l_perm3(items):
'''Generator yielding Lexicographic permutations of a list of items'''
if not items:
yield []
else:
dir = 1
new_items = []
this = [items.pop()]
for item in l_perm3(items):
lenitem = len(item)
try:
# Never insert 'this' above any other 'this' in the item
maxinsert = item.index(this[0])
except ValueError:
maxinsert = lenitem
if dir == 1:
# step down
for new_item in [item[:i] + this + item[i:]
for i in range(lenitem, -1, -1)
if i <= maxinsert]:
yield new_item
else:
# step up
for new_item in [item[:i] + this + item[i:]
for i in range(lenitem + 1)
if i <= maxinsert]:
yield new_item
dir *= -1
from math import factorial
def l_perm_length(items):
'''\
Returns the len of sequence of lexicographic perms of items.
Each item of items must itself be hashable'''
counts = [items.count(item) for item in set(items)]
ans = factorial(len(items))
for c in counts:
ans /= factorial(c)
return ans
if __name__ == '__main__':
n = [0, 1, 2, 2, 2]
print '\nLexicograpic Permutations of %i items: %r' % (len(n), n)
for i, x in enumerate(l_perm3(n[:])):
print('%3i %r' % (i, x))
assert i+1 == l_perm_length(n), 'Generated number of permutations is wrong'
上述程序的输出例如如下:
Lexicograpic Permutations of 5 items: [0, 1, 2, 2, 2]
0 [0, 1, 2, 2, 2]
1 [0, 2, 1, 2, 2]
2 [2, 0, 1, 2, 2]
3 [2, 0, 2, 1, 2]
4 [0, 2, 2, 1, 2]
5 [2, 2, 0, 1, 2]
6 [2, 2, 0, 2, 1]
7 [0, 2, 2, 2, 1]
8 [2, 0, 2, 2, 1]
9 [2, 2, 2, 0, 1]
10 [2, 2, 2, 1, 0]
11 [2, 1, 2, 2, 0]
12 [1, 2, 2, 2, 0]
13 [2, 2, 1, 2, 0]
14 [2, 2, 1, 0, 2]
15 [1, 2, 2, 0, 2]
16 [2, 1, 2, 0, 2]
17 [2, 1, 0, 2, 2]
18 [1, 2, 0, 2, 2]
19 [1, 0, 2, 2, 2]