如何在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` 模組的各種函式從字串中選擇一個專案。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP