如何在 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() 函式(**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 模組的各種函式從字串中選擇一個專案。

更新於: 2022-10-27

9K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.