<p>使用<a href="https://docs.python.org/2/library/random.html#random.shuffle" rel="nofollow noreferrer">random.shuffle</a>对列表进行无序排列(就位)。
然后使用<a href="https://docs.python.org/2/library/itertools.html#itertools.izip" rel="nofollow noreferrer">grouper recipe</a>,<code>zip(*[iterator]*4)</code>将项目收集为4组:</p>
<pre><code>In [32]: import random
In [33]: task_list = ['task_%d' % (i,) for i in range(1, 17)]
In [34]: random.shuffle(task_list)
In [35]: persons = zip(*[iter(task_list)]*4)
In [36]: persons
Out[36]:
[('task_7', 'task_2', 'task_15', 'task_13'),
('task_6', 'task_11', 'task_9', 'task_12'),
('task_5', 'task_10', 'task_4', 'task_1'),
('task_3', 'task_8', 'task_14', 'task_16')]
</code></pre>
<hr/>
<p>好的一面是,石斑鱼的食谱相当优雅。不幸的是,这不是Python中最容易理解的部分。要了解石斑鱼食谱,你需要了解:</p>
<ul>
<li><a href="https://docs.python.org/2/library/functions.html#zip" rel="nofollow noreferrer">zip</a></li>
<li><a href="https://docs.python.org/2/library/functions.html#iter" rel="nofollow noreferrer">iterators</a></li>
<li><a href="https://stackoverflow.com/q/974931/190597">multiplication of a list by an int</a></li>
<li><a href="http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/" rel="nofollow noreferrer">the * unpacking operator</a></li>
</ul>
<p>有了这些概念,就可以找到石斑鱼食谱的解释<a href="https://stackoverflow.com/a/17516752/190597">here</a>。在</p>