numpy.random.RandomState.shuffle#

方法

random.RandomState.shuffle(x)#

通过打乱其内容,就地修改序列。

此函数仅沿多维数组的第一轴打乱数组。子数组的顺序会改变,但其内容保持不变。

注意

新代码应改用 Generator 实例的 shuffle 方法;请参阅快速入门

参数:
xndarray 或 MutableSequence

要打乱的数组、列表或可变序列。

返回:

另请参阅

random.Generator.shuffle

应在新代码中使用。

示例

>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8] # random

多维数组仅沿第一轴打乱

>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5], # random
       [6, 7, 8],
       [0, 1, 2]])