如何在 Python 中从字符串中随机选择一个项目?
在本文中,我们将向您展示如何使用 Python 从字符串中随机选择一个项目。以下是 Python 中完成此任务的各种方法 -
- 使用 random.choice() 方法
- 使用 random.randrange() 方法
- 使用 random.randint() 方法
- 使用 random.random()
- 使用 random.sample() 方法
- 使用 random.choices() 方法
假设我们获取了一个包含一些元素的字符串。我们将使用上面指定的不同方法从给定的输入字符串生成一个随机元素。
方法 1:使用 random.choice() 方法
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
使用 import 关键字导入 random 模块(用于生成随机整数。因为这些是伪随机数,所以它们实际上不是随机的。此模块可用于生成随机数,从列表或字符串中打印随机值等)
创建一个字符串并向其中添加一些虚拟数据。
使用 random.choice() 方法从字符串生成一个随机项(此函数从指定序列(即此处的字符串)返回一个随机元素),将输入字符串作为参数传递给choice() 函数
打印生成的随机字符串项。
示例
以下程序使用 random.choice() 方法从字符串中返回一个随机元素 −
import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random item from the String using random.choice() method randomItem = random.choice(givenString) print("The generated random String item = ", randomItem)
输出
('The given input String: ', 'TutorialsPoint') ('The generated random String item = ', 't')
方法 2:使用 random.randrange() 方法
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 −
使用 random.randrange() 方法从字符串生成随机索引值(返回指定范围内的随机数),方法是使用 len() 函数将输入字符串的长度作为参数传递给它(对象中的项目数由 len() 方法返回)
从字符串中获取上述索引处的元素并创建一个变量来存储它。
示例
以下程序使用 random.randrange() 方法从字符串返回一个随机元素 −
import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random index value by passing the String length to the random.randrange() method randomIndex = random.randrange(len(givenString)) # Getting the element present at the above index from the String randomItem = givenString[randomIndex] print("The generated random String item = ", randomItem)
输出
('The given input String: ', 'TutorialsPoint') ('The generated random String item = ', 'r')
方法 3:使用 random.randint() 方法
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 −
通过使用 len() 函数将输入字符串的长度作为参数传递给它,使用 random.randint() 方法从字符串生成随机索引值(返回指定范围内的随机数)(对象中的项目数由 len() 方法返回)
从字符串中获取上述索引处的元素并创建一个变量来存储它。
示例
以下程序使用 random.randint() 方法从字符串返回一个随机元素 −
import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random index value by passing String length as an argument # to the random.randint() function randomIndex = random.randint(0, len(givenString)-1) # Getting the element present at the above index from the String randomItem = givenString[randomIndex] print("The generated random String item = ", randomItem)
输出
('The given input String: ', 'TutorialsPoint') ('The generated random String item = ', 'i')
方法 4:使用 random.random()
算法(步骤)
使用 random.random() 函数生成一个随机浮点数(随机返回 0 到 1 之间的浮点值),并将其与字符串的长度相乘以获得随机索引,然后使用 int() 函数将结果转换为整数(转换为整数)。
从字符串中获取上述索引处的元素并创建一个变量来存储它。
示例
以下程序使用 random.random() 方法从字符串中返回一个随机元素 −
import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating a random float number and multiplying it with a length # of the String to get a random index and converting it into an integer randomIndex = int(random.random() * len(givenString)) # Getting the element present at the above index from the String randomItem = givenString[randomIndex] print("The generated random String item = ", randomItem)
输出
('The given input String: ', 'TutorialsPoint') ('The generated random String item = ', 'n')
方法 5:使用 random.sample() 方法
算法(步骤)
使用 random.sample() 方法通过传递字符串和要生成的随机项数量作为参数,从字符串中生成所需数量的随机项。
random.sample() 方法返回一个列表,其中包含从序列中随机选择的元素数量。
语法
random.sample(sequence, k)
打印指定数量的随机字符串项生成列表。
示例
以下程序使用 random.sample() 方法从字符串中返回 n 个随机元素 −
import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating 3 random items from the String using random.sample() method randomItems = random.sample(givenString, 3) print("The generated 3 random String items = ", randomItems)
输出
('The given input String: ', 'TutorialsPoint') ('The generated 3 random String items = ', ['o', 'P', 'r'])
方法 6:使用 random.choices() 方法
算法(步骤)
使用 random.choices() 方法从字符串生成所需数量的随机项,方法是将字符串和要生成的随机项数量 (k) 作为参数传递给该方法。
random 模块包含 random.choices() 方法。从字符串中选择多个项目或从特定序列中选择单个项目很有用。
语法
random.choices(sequence, k)
打印指定数量的生成随机字符串项目。
示例
以下程序使用 random.sample() 方法从元组中返回 n 个随机元素 −
import random # input string givenString = "TutorialsPoint" # printing the given input String print("The given input String: ", givenString) # generating 3 random items from String using random.choices() method randomItems = random.choices(givenString, k=3) print("The generated 3 random String items = ", randomItems)
输出
The given input String: TutorialsPoint The generated 3 random String items = ['a', 'o', 'P']
结论
我们学习了如何利用随机模块的各种函数从字符串中选择一个项目。