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

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()函数 (创建一个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()函数去除重复项。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程