如何在 Python 中选择不在列表中的随机数?


在这篇文章中,我们将向您展示如何在 python 中选择不在列表中的随机数。以下是完成此任务的各种方法 -

  • 使用 random.choice() 函数
  • 使用 random.choice() 函数和列表推导式
  • 使用 random.choice() 和 set() 函数
  • 使用 random.randrange() 函数

使用 random.choice() 函数

random.choice() 方法从指定的序列中返回一个随机元素。序列可以是字符串、范围、列表、元组或其他任何内容。

语法

random.choice(sequence)

参数

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

算法(步骤)

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

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

  • 创建一个变量来存储输入列表,并赋予它一些虚拟值。

  • 使用 max() 函数(返回可迭代对象中最高值/最大数)从输入列表中获取最大元素。

  • 创建一个新的列表(空列表)来存储不在给定列表中的值。

  • 使用 for 循环从 0 循环到最大元素。

  • 在循环内部,使用 not in 运算符检查迭代器值是否不在给定列表中。

  • 如果它不在给定列表中,则使用 append() 函数将此元素添加到新列表中。

  • 使用 random.choice() 方法从中选择一个随机元素(此函数从指定的序列即列表中返回一个随机元素)。

  • 打印不在列表中的随机元素。

示例

以下程序使用 random.choice() 函数返回不在输入列表中的随机元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =",inputList) # getting the maximum/greatest element from the list max_element = max(inputList) # Taking an empty list to store elements that are not in the list newList = [] # Looping from 0 to max element using the for loop for i in range(max_element): # Checking whether the number is not in the given list if i not in inputList: # If it is not in the given list then add it to the newList newList.append(i) # Get the random number from the newlist using the choice() function of the random module randomElement = random.choice(newList) # printing the random element which is not in the list print("Random element which is not in the list = ", randomElement)

输出

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

Input List = [3, 10, 5, 9, 2]
Random element which is not in the list = 8

使用 random.choice() 函数和列表推导式

算法(步骤)

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

  • 从 0 循环到最大元素,并使用 not in 运算符选择不在列表中的元素,然后使用 random.choice() 方法从中选择一个随机元素。

  • 打印不在列表中的随机元素。

示例

以下程序使用 random.choice() 函数和列表推导式返回不在输入列表中的随机元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =",inputList) # getting the maximum/greatest element from the list max_element = max(inputList) # Get all the numbers from 0 to max_element that are not present in the list # Selecting a random element from those elements using the choice() function randomElement = random.choice([e for e in range(max_element) if e not in inputList]) # printing the random element which is not in the list print("The random element which is not in the list = ", randomElement)

输出

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

Input List = [3, 10, 5, 9, 2]
The random element which is not in the list =  7

使用 random.choice() 和 set() 函数

算法(步骤)

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

  • 使用 set() 函数(创建集合对象。集合列表将以随机顺序出现,因为项目没有排序。它删除所有重复项)和 range() 函数,获取从输入列表的最低元素到最高元素的数字集合。从输入列表的集合中减去此集合以删除公共元素,然后将其转换为列表,并使用 random.choice() 函数从中选择一个随机元素。

  • 打印不在列表中的随机元素

示例

以下程序使用 random.choice() 和 set() 函数返回 python 中给定输入列表中不存在的随机元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List = ",inputList) # Getting the set of numbers from min element of the input list to the maximum element using set() and range() functions # Subtracting this set from the set of the input list to remove the common elements # Converting this to a list and selecting a random element from this list randomElement = random.choice(list(set(range(min(inputList)+1, max(inputList)))-set(inputList))) # printing the random element which is not in the list print("Random element which is not in the list = ", randomElement)

输出

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

Input List =  [3, 10, 5, 9, 2]
Random element which is not in the list =  4

使用 random.randrange() 函数

算法(步骤)

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

  • 使用 while 循环通过将条件设置为 True 来无限次运行循环。

  • 使用 randrange() 函数(返回指定范围内的随机数)获取 1 到 100 之间的随机元素。

  • 使用 if 条件语句使用 not 和 in 运算符检查生成的随机元素是否不在列表中。

  • 如果条件为真,则使用 break 语句跳出循环(这意味着我们找到了一个不在给定列表中的元素)。

  • 打印不在列表中的随机元素

示例

以下程序使用 random.randrange() 函数返回 python 中给定输入列表中不存在的随机元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =", inputList) # Running the loop infinite times while True: # getting the random element from 1 to 100 randomElement=random.randrange(1, 100) # checking whether the generated random element is not present in the list if randomElement not in inputList: # breaking the loop if the condition is true break # printing the random element which is not in the list print("The random element which is not in the list = ", randomElement)

输出

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

Input List = [3, 10, 5, 9, 2]
The random element which is not in the list =  96

结论

在这篇文章中,我们学习了如何使用 Python 的四种不同的方法来选择不在给定列表中的随机元素。我们还学习了如何使用 set() 函数删除重复项。

更新于:2022-10-27

2K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.