Python 如何随机选择字符串中的一个项目

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()方法

步骤

以下是执行所需任务所需遵循的算法/步骤−

  • 使用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']

结论

我们学习了如何利用random模块的各种函数从字符串中选择一个项目。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程