1. 对列表进行乱序
通过 random.shuffle() 方法实现,直接对列表进行操作
>>> import random >>> a = list(range(4)) >>> a [0, 1, 2, 3] >>> random.shuffle(a) >>> a [3, 0, 1, 2]
2. 获取指定范围的随机数
通过 random.uniform() 方法实现
>>> random.uniform(0,10) 2.3591479768289227 >>> random.uniform(9,10) 9.402797721231435
3. 将两个列表合并起来
通过 zip() 函数实现
>>> import random >>> a = list(range(4)) >>> a [2, 1, 0, 3] >>> b = a.copy() >>> random.shuffle(b) >>> b [2, 3, 0, 1] >>> list(zip(a,b)) [(2, 2), (1, 3), (0, 0), (3, 1)] >>> ba = list(zip(a,b)) >>> ba [(2, 2), (1, 3), (0, 0), (3, 1)] >>> c [2, 1, 0, 3] >>> list(zip(ba, c)) [((2, 2), 2), ((1, 3), 1), ((0, 0), 0), ((3, 1), 3)]
4. 合并后的分离
通过 zip(*) 来实现
>>> ba [(2, 2), (1, 3), (0, 0), (3, 1)] >>> m, n = zip(*ba) >>> m (2, 1, 0, 3) >>> n (2, 3, 0, 1)