用Python自带的shuffle函数打乱numpy数组的时候,居然修改了原数组的值,而列表就没有遇到类似的问题。
from random import shuffle
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
c = np.hstack((a, b))
shuffle(c)
print(c)
d = [1, 2, 3, 4, 5, 6, 7, 8, 9]
aa = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
shuffle(d)
shuffle(aa)
print(a)
print(aa)
# 运行结果
[[1 2 3 1 1 1]
[4 5 6 2 2 2]
[1 2 3 1 1 1]]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[4, 5, 6], [7, 8, 9], [1, 2, 3]]
解决办法,numpy数组用numpy自带的random函数吧,此外还要注意到Python浅拷的问题
import numpy as np
np.random.shuffle()