转载请注明出处
个人博客:https://maxusun.github.io/
在实际开发中,我们需要以相同的方法,同时打乱多个list
,这里整理了几个常用的方法:
1. 使用zip
import random
c = list(zip(a,b)) # 将a,b整体作为一个zip,每个元素一一对应后打乱
random.shuffle(c) # 打乱c
a[:],b[:] = zip(*c) # 将打乱的c解开
2. numpy中的permutation函数
a = np.asarray([1,2,3,4,5,6)
b = np.asarray([6,5,4,3,2,1])
permutation = np.random.permutation(a.shape[0]) # 利用np.random.permutaion函数,获得打乱后的行数,输出permutation
a = a[permutaion] # 得到打乱后数据a
b = b[permutation] # 得到打乱后数据b
3. 使用numpy中的state
state = np.random.get_state()
np.random.shuffle(a)
np.random.set_state(state)
np.random.shuffle(b)