在不评估完整迭代器的情况下获取特定排列 (itertools.permutation)
Get specific permutation without evaluating the full iterator (itertools.permutation)
我想访问几个固定样本来排列长列表。显然我可以做到:
In [18]: import itertools
In [19]: l = [p for p in itertools.permutations(range(10))]
In [20]: len(l)
Out[20]: 3628800
In [21]: l[256766]
Out[21]: (0, 7, 3, 9, 5, 6, 4, 2, 1, 8)
但这会导致对长列表 l
进行评估以创建列表。对于 10 项列表仍然是可能的。挂起更大的列表。
有没有办法在不创建完整列表的情况下通过调用其编号来获得特定排列?
请注意,我不想随机播放。我希望它是相同的排列,最好是 "permutation number" 匹配使用 itertools.permutations.
时调用的列表位置
编辑:回复:重复。也欢迎与 itertools 模块相关的答案(参见下面的讨论)。因此,虽然目标与 Ranking and unranking of permutations with duplicates 中的目标相同,但在 itertools 上下文中的讨论可能仍然是值得的。
您可以使用 itertools.islice()
:
def get(perms, index):
return next(itertools.islice(perms, index, index+1))
请注意,这将部分耗尽迭代器。这意味着您不能在同一个迭代器上多次执行此操作。
您也可以创建自己的 class 以便它可以记住已经生成的值。这样,您可以在同一对象上找到多个索引:
class Perms:
def __init__(self, iterable, r=None):
self.perm = itertools.permutations(iterable, r)
self.generated = []
self.next = self.__next__ # Python 2 compatibility
def __next__(self):
value = next(self.perm)
self.generated.append(value)
return value
def __getitem__(self, index):
if index < 0:
self.generated.extend(self.perm)
else:
gen_length = len(self.generated)
for _ in range(index - gen_length + 1):
next(self)
return self.generated[index]
不太难:
def nthperm(l, n):
l = list(l)
indices = []
for i in xrange(1, 1+len(l)):
indices.append(n % i)
n //= i
indices.reverse()
perm = []
for index in indices:
# Using pop is kind of inefficient. We could probably avoid it.
perm.append(l.pop(index))
return tuple(perm)
这里的想法是,列表 l
的第 n
次排列从项目 n // factorial(len(l) - 1)
开始,然后继续列表的剩余元素的第 n % factorial(len(l) - 1)
次排列l
.
如果你测试它,你会发现它确实有效:
>>> all(perm == nthperm(range(5), i) for i, perm in enumerate(itertools.permutations(range(5))))
True
而且对于迭代 itertools.permutations
永远不会完成的输入,它的工作速度足够快:
>>> nthperm(range(100), factorial(100) // 2)
(50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 2
1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 4
1, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 6
2, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 8
2, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99)
我想访问几个固定样本来排列长列表。显然我可以做到:
In [18]: import itertools
In [19]: l = [p for p in itertools.permutations(range(10))]
In [20]: len(l)
Out[20]: 3628800
In [21]: l[256766]
Out[21]: (0, 7, 3, 9, 5, 6, 4, 2, 1, 8)
但这会导致对长列表 l
进行评估以创建列表。对于 10 项列表仍然是可能的。挂起更大的列表。
有没有办法在不创建完整列表的情况下通过调用其编号来获得特定排列?
请注意,我不想随机播放。我希望它是相同的排列,最好是 "permutation number" 匹配使用 itertools.permutations.
时调用的列表位置编辑:回复:重复。也欢迎与 itertools 模块相关的答案(参见下面的讨论)。因此,虽然目标与 Ranking and unranking of permutations with duplicates 中的目标相同,但在 itertools 上下文中的讨论可能仍然是值得的。
您可以使用 itertools.islice()
:
def get(perms, index):
return next(itertools.islice(perms, index, index+1))
请注意,这将部分耗尽迭代器。这意味着您不能在同一个迭代器上多次执行此操作。
您也可以创建自己的 class 以便它可以记住已经生成的值。这样,您可以在同一对象上找到多个索引:
class Perms:
def __init__(self, iterable, r=None):
self.perm = itertools.permutations(iterable, r)
self.generated = []
self.next = self.__next__ # Python 2 compatibility
def __next__(self):
value = next(self.perm)
self.generated.append(value)
return value
def __getitem__(self, index):
if index < 0:
self.generated.extend(self.perm)
else:
gen_length = len(self.generated)
for _ in range(index - gen_length + 1):
next(self)
return self.generated[index]
不太难:
def nthperm(l, n):
l = list(l)
indices = []
for i in xrange(1, 1+len(l)):
indices.append(n % i)
n //= i
indices.reverse()
perm = []
for index in indices:
# Using pop is kind of inefficient. We could probably avoid it.
perm.append(l.pop(index))
return tuple(perm)
这里的想法是,列表 l
的第 n
次排列从项目 n // factorial(len(l) - 1)
开始,然后继续列表的剩余元素的第 n % factorial(len(l) - 1)
次排列l
.
如果你测试它,你会发现它确实有效:
>>> all(perm == nthperm(range(5), i) for i, perm in enumerate(itertools.permutations(range(5))))
True
而且对于迭代 itertools.permutations
永远不会完成的输入,它的工作速度足够快:
>>> nthperm(range(100), factorial(100) // 2)
(50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 2
1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 4
1, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 6
2, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 8
2, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99)