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