Python 如何打乱对象列表

Python 如何打乱对象列表

在本文中,我们将向您展示如何在Python中打乱对象列表。以下是完成此任务的各种方法:

  • 使用random.shuffle()函数

  • 使用random.sample()函数

  • 使用Fisher-Yates洗牌算法

  • 使用random.randint()和pop()函数

假设我们有一个包含一些元素的列表。我们将使用上述不同的方法随机打乱列表的元素。

使用random.shuffle()函数

在random模块中,shuffle()方法用于打乱一个列表。它接受一个序列(比如列表),并重新组织项目的顺序。

这个shuffle()方法会更改原始列表,它不会返回一个新列表。列表的顺序在这个过程中丢失,这是这种方法的唯一缺点。

语法

random.shuffle(sequence, function)

参数

  • sequence - 任何序列,如列表、元组等。

  • function(optional) - 返回一个介于0.0和1.0之间值的函数名称。如果未指定函数,则使用random()函数。

步骤

执行所需任务的算法/步骤如下:

  • 使用import关键字导入random模块。

  • 创建一个变量以存储输入的列表。

  • 打印输入列表。

  • 使用random.shuffle()函数将所有列表元素随机洗牌,将列表作为参数传递给该函数。

  • 打印结果洗牌后的列表。

示例

以下程序使用random.shuffle()函数返回洗牌后的列表。

# importing random module
import random
# input list
inputList = [3, 10, 5, 9, 2]
# Printing the input list
print ("Input list: ", inputList)
# shuffling the list of elements using the random module shuffle function
random.shuffle(inputList)
# Printing the shuffled list
print ("Shuffled list: ", inputList)

输出

执行上述程序后,将生成以下输出:

Input list: [3, 10, 5, 9, 2]
Shuffled list: [9, 3, 10, 2, 5]

使用random.sample()函数

在python中, random.sample() 方法返回一个新的乱序列表。原始列表保持不变。

random.sample()方法返回一个包含从序列中随机选择的元素数量的列表。

语法

random.sample(sequence, k)

参数

  • sequence - 任何类似于列表、元组等的序列

  • k - 要返回的元素的数量

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字导入 random 模块

  • 使用 random.sample() 函数通过传递输入列表和输入列表的长度来随机打乱所有列表元素,长度可以使用len()函数获得(len()方法返回对象中的项数)

  • 打印结果的打乱列表

示例

以下程序使用random.shuffle()函数返回打乱的列表:

# importing random module
import random
# input list
inputList = [3, 10, 5, 9, 2]
# Printing the input list
print ("Input list: ", inputList)
# shuffling the list elements using the sample function
shuffledList = random.sample(inputList, len(inputList))
# Printing the shuffled list
print ("Shuffled list: ", shuffledList)

输出

执行上述程序后将生成以下输出:

Input list: [3, 10, 5, 9, 2]
Shuffled list: [2, 5, 9, 10, 3]

使用Fisher-Yates洗牌算法

这是Python中一个众所周知的算法,用于对一系列数字进行洗牌。

Fisher-Yates洗牌算法

Fisher-Yates洗牌算法的时间复杂度为O(n)。假设我们有一个rand()函数,它可以在O(1)时间内生成一个随机数。算法的思想是从最后一个元素开始,将其与整个数组(包括最后一个元素)中随机选择的一个元素交换。然后将数组从0到n-2(大小减少一个)进行重复此过程,直到达到第一个元素。

步骤

以下是执行所需任务的算法/步骤:

  • 使用for循环从列表的末尾开始遍历,使用len()函数(返回对象中的项数)。

  • 使用random.randint()方法获得当前索引值的随机索引(返回指定范围内的随机数)。

  • 交换当前索引元素与随机索引处的元素。

  • 打印结果洗牌后的列表。

示例

以下程序使用Fisher-Yates洗牌算法返回洗牌后的列表:

# importing random module
import random
# input list
inputList = [3, 10, 5, 9, 2]
# Printing the input list
print ("Input list: ", inputList)
# traversing from the end of the list(In reverse order)
for p in range(len(inputList)-1, 0, -1):
   # getting a random index from 0 to the current index
   q = random.randint(0, p + 1)
   # Swap the current index element with the element at a random index
   inputList[p], inputList[q] = inputList[q], inputList[p]
# Printing the shuffled list
print ("Shuffled list: ", inputList)

输出

执行上述程序后,将生成以下输出:

Input list: [3, 10, 5, 9, 2]
Shuffled list: [9, 10, 3, 5, 2]

使用random.randint()和pop()函数

random.randint() - 返回指定范围内的随机数

示例

以下程序使用random.randint()和pop()函数返回一个打乱顺序的列表 –

# importing random module
import random
# input list
inputList = [3, 10, 5, 9, 2]
# Printing the input list
print("Input list: ", inputList)
# getting the list length
listLength = len(inputList)
# repeating the loop till the length of the list
for i in range(listLength):
   # getting a random index in the range 0 and list Length - 1
   randomIndex = random.randint(0, listLength-1)
   # deleting the element at that corresponding index from the list
   ele= inputList.pop(randomIndex)
   # appending the above-deleted element to the input list(adding the element at last)
   inputList.append(ele)
# Printing the shuffled list
print ("Shuffled list: ", inputList)

输出

在执行上述程序时,会生成以下输出结果 –

Input list: [3, 10, 5, 9, 2]
Shuffled list: [10, 2, 3, 5, 9]

结论

在这篇文章中,我们学习了四种不同的Python方法来打乱给定的列表。我们还学习了用于打乱列表的Fisher-Yates算法以及如何在Python中使用它。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程