擅长:python、mysql、java
<p>假设我们有<code>lines</code>变量和数据集的迭代器。然后:</p>
<pre><code>from random import sample
# Chunk length
chunk_len = 2
# Number of chunks
num_of_chunks = 5
# Get the sample with data for all chunks. It guarantees us that there will
# be no repetitions
random_sample = sample(lines, num_of_chunks*chunk_len)
# Construct the list with chunks
result = [random_sample[i::num_of_chunks] for i in range(num_of_chunks)]
result
</code></pre>
<p>将返回:</p>
<pre><code>[['123 QX20765B 1 1',
'118 GN31829N 1 1'],
['127 IT17216C 1 1',
'122 YW35494W 1 1'],
['138 LI89002Q 1 1',
'126 RG21661H 1 1'],
['120 VJ71307A 1 1',
'121 OX22563R 1 1'],
['143 OW40240P 1 1',
'142 XJ75065T 1 1']]
</code></pre>