如何在 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()** 函式(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 列表轉換為元組。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP