你如何产生的Python列表的所有排列,独立的元素在该列表类型的?
例如:
排列([])
[]
置换([1])
[1]
置换([1,2])
[1,2]
[2,1]
置换([1,2,3])
[1,2,3]
[1,3,2]
[2,1,3]
[2,3,1]
[3,1,2]
[3,2,1]
编辑: Eliben指出了一个解决方案,这是与我相似,虽然比较简单,所以我选择它作为公认的答案,虽然Python的2.6+有 itertools 模块中内置的解决方案:
进口itertools
和itertools.permutations([1,2,3])
解决方案
与Python 2.6开始(如果你在Python 3中),你有一个标准库工具这样的:itertools.permutations.
如果您使用的是较老的Python(小于2.6)由于某种原因或只是好奇,想知道它是如何工作的,这里有一个很好的方法,从http://$c$c.activestate.com/recipes/252178/:
高清all_perms(元素):
如果len(元素)LT = 1:
产量要素
其他:
用于烫发的all_perms(元素[1:]):
因为我在范围内(LEN(元素)):
#NB元素[0:1]在这两个字符串和列表环境工程
产生烫发[我] +元素[0:1] +烫发[我:]
一对夫妇的替代方法列和itertools.permutations
的文档。这里有一个:
高清排列(迭代器,R =无):
#置换(ABCD,2) - > AB AC AD BA BC BD CA CB CD DA DB DC
#置换(范围(3)) - > 012 021 102 120 201 210
池=元组(迭代器)
N = LEN(池)
R =正,如果r为无其他ř
在r> N:
返回
指数=范围(N)
周期=范围(正,N-R 1,-1)
产量元组(池[我]因为我在指数[:R])
而N:
因为我在逆转(范围(R)):
周期[I] - 1 =
如果周期[我] == 0:
指数为[I:] =指数为[I + 1:] +指数为[I:I + 1]
周期[我] = N - 我
其他:
J =周期[I]
指数为[I]中,指数[-j] =指数为[-j],指数为[I]
产量元组(池[我]因为我在指数[:R])
打破
其他:
返回
和其它的基础上, itertools.product
:
高清排列(迭代器,R =无):
池=元组(迭代器)
N = LEN(池)
R =正,如果r为无其他ř
在产品指数(范围(n)时,重复= r)的:
如果len(集(指数))== R:
产量元组(池[I]为我的索引)
How do you generate all the permutations of a list in Python, independently of the type of elements in that list?
For example:
permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
EDIT: Eliben pointed to a solution that's similar to mine although simpler, so I'm choosing it as the accepted answer, although Python 2.6+ has a builtin solution in the itertools module:
import itertools
itertools.permutations([1, 2, 3])
解决方案
Starting with Python 2.6 (and if you're on Python 3) you have a standard-library tool for this: itertools.permutations
.
If you're using an older Python (<2.6) for some reason or are just curious to know how it works, here's one nice approach, taken from http://code.activestate.com/recipes/252178/:
def all_perms(elements):
if len(elements) <=1:
yield elements
else:
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
# nb elements[0:1] works in both string and list contexts
yield perm[:i] + elements[0:1] + perm[i:]
A couple of alternative approaches are listed in the documentation of itertools.permutations
. Here's one:
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = range(n)
cycles = range(n, n-r, -1)
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
And another, based on itertools.product
:
def permutations(iterable, r=None):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)