Python 如何从元组中随机选择一个项

Python 如何从元组中随机选择一个项

在本文中,我们将展示如何使用Python从元组中随机选择一个项。以下是在Python中完成此任务的各种方法:

  • 使用random.choice()方法

  • 使用random.randrange()方法

  • 使用random.randint()方法

  • 使用random.random()方法

  • 使用random.sample()方法

  • 使用random.choices()方法

假设我们有一个包含一些元素的元组。我们将使用上述不同的方法从给定的输入元组中生成一个随机元素。

使用random.choice()方法

步骤

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

  • 使用import关键字导入random模块(用于生成随机整数。因为这些是伪随机数,所以它们实际上不是随机的。该模块可用于生成随机数,并从元组、列表或字符串等中打印随机值)。

  • 创建一个元组并向其添加一些虚拟数据。

  • 使用random.choice()方法从元组中生成一个随机项(该函数通过将输入元组作为参数传递给choice()函数来返回指定序列即元组中的随机元素)。

  • 打印生成的随机元组项。

示例

以下程序使用random.choice()方法从元组中返回一个随机元素:

# importing random module
import random
# input tuple
inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code")
# printing the given input tuple
print("The given input tuple: ", inputTuple)
# generating a random item from the tuple using random.choice() method
randomItem = random.choice(inputTuple)
print("The generated random tuple item = ", randomItem)

输出

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

The given input tuple: (30, 10, 'TutorialsPoint', 20, 'python', 'code')
The generated random tuple item = 20

使用random.randrange()方法

步骤

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

  • 使用 random.randrange() 方法从元组中生成随机索引值(通过将输入元组的长度作为参数传递给它, len() 函数返回对象中的项目数)

  • 获取上述索引处的元素,并创建一个变量来存储它

示例

以下程序使用random.randrange()方法从元组中返回一个随机元素:

# importing random module
import random
# input tuple
inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code")
# generating a random index value by passing the tuple length to the random.randrange() method
randomIndex = random.randrange(len(inputTuple))
# Getting the element present at the above index from the tuple
randomItem = inputTuple[randomIndex]
print("The generated random tuple item = ", randomItem)

输出

执行上述程序后,将会产生以下输出 –

The generated random tuple item = TutorialsPoint

使用random.randint()方法

步骤

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

  • 使用random.randint()方法从元组中生成一个随机索引值 (在指定范围内返回一个随机数),将输入元组的长度作为参数传递给len()函数(通过len()方法返回对象的项目数)

  • 获取上述索引处的元素并创建一个变量来存储它。

示例

以下程序使用random.randint()方法从元组中返回一个随机元素 –

# importing random module
import random
# input tuple
inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code")
# generating a random index value by passing tuple length as an argument
# to the random.randint() function
randomIndex = random.randint(0, len(inputTuple)-1)
# Getting the element present at the above index from the tuple
randomItem = inputTuple[randomIndex]
print("The generated random tuple item = ", randomItem)

输出

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

The generated random tuple item = code

使用random.random()

步骤

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

  • 使用 random.random() 函数(返回0到1之间的随机浮点数)生成一个随机浮点数,然后将其乘以元组的长度以获取一个随机索引,并使用 int() 函数将结果转换为整数(转换为整数)。

  • 从元组中获取上述索引处的元素并创建一个变量以存储它。

示例

以下程序使用random.random()方法从元组中返回一个随机元素-

# importing random module
import random
# input tuple
inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code")
# generating a random float number and multiplying it with a length
# of the tuple to get a random index and converting it into an integer
randomIndex = int(random.random() * len(inputTuple))
# Getting the element present at the above index from the tuple
randomItem = inputTuple[randomIndex]

print("The generated random tuple item = ", randomItem)

输出

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

The generated random tuple item = 20

使用random.sample()方法

步骤

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

  • 使用random.sample()方法,通过将元组和要生成的随机项数作为参数传递给它来从元组中生成所需数量的随机项。
The random.sample() method returns a list containing a randomly selected number of elements from a sequence.
Syntax:
random.sample(sequence, k)
  • 使用tuple()函数将列表转换为元组,并打印出来。

示例

下面的程序使用random.sample()方法从元组中返回n个随机元素-

# importing random module
import random
# input tuple
inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code")
# generating 3 random items from the tuple using random.sample() method
randomItems = random.sample(inputTuple, 3)
print("The generated 3 random tuple items = ", tuple(randomItems))

输出

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

The generated 3 random tuple items = (20, 'TutorialsPoint', 30)

使用random.choices()方法

步骤

以下是完成所需任务的算法/步骤-。

  • 使用 random.choices() 方法从元组中生成所需数量的随机项,将元组和要生成的随机项的数量(k)作为参数传递给它。
The random module contains the random.choices() method. It is useful to select multiple items from a list or a single item from a specific sequence.
Syntax:
random.choices(sequence, k)
  • 使用tuple()函数将列表转换为元组,并打印出来。

示例

以下程序使用random.sample()方法从元组中返回n个随机元素 –

import random
givenTuple = ("Hello", 10, "TutorialsPoint", 20, "python", "code")
# generating 3 random items from a tuple using random.choices() method
randomItems = random.choices(givenTuple, k=3)
# Converting the random elements list to a tuple
print("The generated 3 random tuple items = ", tuple(randomItems))

输出

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

The generated 3 random tuple items = (10, 'python', 'Hello')

结论

在本文中,我们学习了如何使用六种不同的方法从Python元组中随机选择元素。我们还学习了如何将Python列表转换为元组。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程