Python 为什么 random.shuffle 返回 None?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17649875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does random.shuffle return None?
提问by alvas
Why is random.shuffle
returning None
in Python?
为什么在 Python 中random.shuffle
返回None
?
>>> x = ['foo','bar','black','sheep']
>>> from random import shuffle
>>> print shuffle(x)
None
How do I get the shuffled value instead of None
?
我如何获得洗牌后的值而不是None
?
采纳答案by Martijn Pieters
random.shuffle()
changes the x
list in place.
random.shuffle()
更改x
列表到位。
Python API methods that alter a structure in-place generally return None
, not the modified data structure.
就地更改结构的 Python API 方法通常返回None
,而不是修改后的数据结构。
If you wanted to create a newrandomly-shuffled list based of an existing one, where the existing list is kept in order, you could use random.sample()
with the full length of the input:
如果您想根据现有列表创建一个新的随机混洗列表,其中现有列表按顺序排列,您可以使用random.sample()
输入的全长:
x = ['foo', 'bar', 'black', 'sheep']
random.sample(x, len(x))
You could also use sorted()
with random.random()
for a sorting key:
您还可以使用sorted()
withrandom.random()
作为排序键:
shuffled = sorted(x, key=lambda k: random.random())
but this invokes sorting (an O(NlogN) operation), while sampling to the input length only takes O(N) operations (the same process as random.shuffle()
is used, swapping out random values from a shrinking pool).
但这会调用排序(O(NlogN) 操作),而对输入长度的采样只需要 O(N) 操作(与使用的过程相同random.shuffle()
,从收缩池中换出随机值)。
Demo:
演示:
>>> import random
>>> x = ['foo', 'bar', 'black', 'sheep']
>>> random.sample(x, len(x))
['bar', 'sheep', 'black', 'foo']
>>> sorted(x, key=lambda k: random.random())
['sheep', 'foo', 'black', 'bar']
>>> x
['foo', 'bar', 'black', 'sheep']
回答by alecxe
According to docs:
根据文档:
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().
将序列 x 原地打乱。可选参数 random 是一个 0 参数函数,返回 [0.0, 1.0) 中的随机浮点数;默认情况下,这是函数 random()。
>>> x = ['foo','bar','black','sheep']
>>> from random import shuffle
>>> shuffle(x)
>>> x
['bar', 'black', 'sheep', 'foo']
回答by Lutz Prechelt
Why, really?
为什么,真的?
1. Efficiency
1. 效率
shuffle
modifies the list in place.
This is nice, because copying a large list would be pure overhead if you do not need the original list anymore.
shuffle
就地修改列表。这很好,因为如果您不再需要原始列表,复制大列表将是纯粹的开销。
2. Pythonic style
2. Pythonic 风格
According to the "explicit is better than implicit"principle of pythonic style, returning the list would be a bad idea, because then one might think it isa new one although in fact it is not.
根据pythonic风格的“显式优于隐式”原则,返回列表将是一个坏主意,因为这样人们可能会认为它是一个新的,但实际上并非如此。
But I don't like it like this!
但我不喜欢这样!
If you doneed a fresh list, you will have to write something like
如果你确实需要一个新的列表,你将不得不写一些类似的东西
new_x = list(x) # make a copy
random.shuffle(new_x)
which is nicely explicit.
If you need this idiom frequently, wrap it in a function shuffled
(see sorted
) that returns new_x
.
这是非常明确的。如果你需要这个成语频繁,包装在一个函数shuffled
(见sorted
),其收益new_x
。
回答by Acemad
This method works too.
这个方法也有效。
import random
shuffled = random.sample(original, len(original))
回答by litepresence
I had my aha moment with this concept like this:
我有这样的概念的啊哈时刻:
from random import shuffle
x = ['foo','black','sheep'] #original list
y = list(x) # an independent copy of the original
for i in range(5):
print shuffle(y) # shuffles the original "in place" prints "None" return
print x,y #prints original, and shuffled independent copy
>>>
None
['foo', 'black', 'sheep'] ['foo', 'black', 'sheep']
None
['foo', 'black', 'sheep'] ['black', 'foo', 'sheep']
None
['foo', 'black', 'sheep'] ['sheep', 'black', 'foo']
None
['foo', 'black', 'sheep'] ['black', 'foo', 'sheep']
None
['foo', 'black', 'sheep'] ['sheep', 'black', 'foo']
回答by Gangil Seo
shuffle(x)
does not return any values. Instead, that function shuffle the variable itself.
不返回任何值。相反,该函数对变量本身进行混洗。
So don't try
所以不要尝试
print shuffle(x)
instead just print the variable like this.
而是像这样打印变量。
>>> x = ['foo','bar','black','sheep']
>>> from random import shuffle
>>> shuffle(x)
['bar', 'black', 'foo', 'sheep']
回答by devsaw
You can return the shuffled list using random.sample()
as explained by others. It works by sampling k elements from the list without replacement.
So if there are duplicate elements in your list, they will be treated uniquely.
您可以random.sample()
按照其他人的解释使用返回洗牌后的列表。它的工作原理是从列表中采样 k 个元素而无需替换。因此,如果您的列表中有重复的元素,它们将被唯一处理。
>>> l = [1,4,5,3,5]
>>> random.sample(l,len(l))
[4, 5, 5, 3, 1]
>>> random.sample(l,len(l)-1)
[4, 1, 5, 3]
>>> random.sample(l,len(l)-1)
[3, 5, 5, 1]
回答by user3467537
Python APIs which change the structure in place itself returns Noneas output.
改变结构的 Python API 本身返回None作为输出。
list = [1,2,3,4,5,6,7,8]
print(list)
Output: [1, 2, 3, 4, 5, 6, 7, 8]
输出:[1, 2, 3, 4, 5, 6, 7, 8]
from random import shuffle
print(shuffle(list))
Output: None
输出:无
from random import sample
print(sample(list, len(list)))
Output: [7, 3, 2, 4, 5, 6, 1, 8]
输出:[7, 3, 2, 4, 5, 6, 1, 8]
回答by frank stockmans
>> x = ['foo','bar','black','sheep']
>> random.shuffle(x)
>> print(x)
>> ['sheep', 'bar', 'foo', 'black']
As pointed out random.shuffle replaces in place, so you wouldn't need a new list variable.
正如指出 random.shuffle 替换到位,所以你不需要一个新的列表变量。