Skip to main content
 首页 » 编程设计

python之numpy.random.shuffle 返回 None

2025年05月04日11fff_TT

我安装了 numpy1.8.2 然后我尝试了下面的代码:

import numpy as np 
a = np.arange(10) 
print a, np.random.shuffle(a) 

但它的输出是:

[0 1 2 3 4 5 6 7 8 9] 无

根据它的 doc,我不知道它为什么返回 None它应该工作!我无法弄清楚问题所在。

我在 Windows 7 上使用 PyCharm 3.1。

请您参考如下方法:

shuffle 就地工作,因此不返回值。

In [1]: x = range(9) 
 
In [2]: x 
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8] 
 
In [5]: print numpy.random.shuffle(x) 
None 
 
In [6]: x 
Out[6]: [8, 7, 3, 4, 6, 0, 5, 1, 2]