【发布时间】:2017-11-06 11:53:33
【问题描述】:
关于随机模块的random.shuffle方法,有不少questions堆栈溢出。
shuffle 让我恼火的是,它在原地洗牌,而不是返回一个洗牌的副本。
请注意,shuffle 可以正常工作,并返回 None。
因此表达类似
for index, (parent1, parent2) in enumerate(zip(sorted(population)[::2], shuffle(population)[1::2])):
不工作。用副作用编写它似乎不必要地冗长:
other_half = population[1::2]
random.shuffle(other_half)
for index, (parent1, parent2) in enumerate(zip(sorted(population)[::2], other_half):
在功能上洗牌列表的pythonic方法是什么?
【问题讨论】: