自己写函数用于随机打乱列表中的元素
随机选取原列表索引,将索引位置上的值进行交换
import random
def random_list1(li):
for i in range(0, 100):
index1 = random.randint(0, len(li) - 1)
index2 = random.randint(0, len(li) - 1)
li[index1], li[index2] = li[index2], li[index1]
return li
li = [1, 2, 3, 4, 5]
test = random_list1(li)
print(test)
首先生成原列表的拷贝a_copy,新建一个空列表result,然后随机选取拷贝列表中的值存入空列表result,然后删除
import random
def random_list2(a):
a_copy = a.copy()
result = []
count = len(a)
for i in range(0, count):
index = random.randint(0, len(a_copy) - 1)
result.append(a_copy[index])
del a_copy[index]
return result
test = [1, 3, 4, 5, 6]
result = random_list2(test)
print(result)
import random
test = [1, 2, 3, 4, 5]
random.shuffle(test)
print(test)
Python的random.shuffle()函数可以用来乱序序列,它是在序列的本身打乱,而不是新生成一个序列。
def shuffle(self, x, random=None):
"""Shuffle list x in place, and return None.
Optional argument random is a 0-argument function returning a
random float in [0.0, 1.0); if it is the default None, the
standard random.random will be used.
"""
if random is None:
randbelow = self._randbelow
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = randbelow(i + 1)
x[i], x[j] = x[j], x[i]
else:
_int = int
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = _int(random() * (i + 1))
x[i], x[j] = x[j], x[i]