回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图从索引列表中生成置换,目前,我使用<code>itertools.permutation</code>。没关系,只是我需要一个真正随机的索引,因为<strong>我不能选择所有的排列,而是选择整个集合(初始的)的一个很短的子集进行模拟</p>
<p>对于<code>itertools.permutation</code>:
<em>置换元组根据输入可数的顺序按字典顺序发出。因此,如果对输入iterable进行排序,则组合元组将按排序顺序生成</em></p>
<pre><code>import itertools
for ind, idxs in enumerate(itertools.permutations(range(5))):
print(ind)
print(idxs)
print('--------')
</code></pre>
<pre><code>0
(0, 1, 2, 3, 4)
--------
1
(0, 1, 2, 4, 3)
--------
2
(0, 1, 3, 2, 4)
--------
3
(0, 1, 3, 4, 2)
--------
4
(0, 1, 4, 2, 3)
--------
5
(0, 1, 4, 3, 2)
--------
6
(0, 2, 1, 3, 4)
--------
7
(0, 2, 1, 4, 3)
--------
8
(0, 2, 3, 1, 4)
--------
9
(0, 2, 3, 4, 1)
--------
10
(0, 2, 4, 1, 3)
--------
11
(0, 2, 4, 3, 1)
--------
12
(0, 3, 1, 2, 4)
--------
13
(0, 3, 1, 4, 2)
--------
</code></pre>
<p>我想到的一个解决方案肯定是每次洗牌列表以获得一个随机顺序,但这使得排列的想法过时,这是不可取的,因为有可能会多次生成相同的样本。排列应该以迭代的方式生成,所以我不能只做<code>list(itertools.permutation..)</code>,因为这将产生一个真正不必要的长列表</p>