如何在 Python 中選擇不在列表中的隨機數?


在這篇文章中,我們將向您展示如何在 python 中選擇不在列表中的隨機數。以下是完成此任務的各種方法 -

  • 使用 random.choice() 函式
  • 使用 random.choice() 函式和列表推導式
  • 使用 random.choice() 和 set() 函式
  • 使用 random.randrange() 函式

使用 random.choice() 函式

random.choice() 方法從指定的序列中返回一個隨機元素。序列可以是字串、範圍、列表、元組或其他任何內容。

語法

random.choice(sequence)

引數

sequence − 任何序列,如列表、元組等。

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用 import 關鍵字匯入 random 模組。

  • 建立一個變數來儲存輸入列表,並賦予它一些虛擬值。

  • 使用 max() 函式(返回可迭代物件中最高值/最大數)從輸入列表中獲取最大元素。

  • 建立一個新的列表(空列表)來儲存不在給定列表中的值。

  • 使用 for 迴圈從 0 迴圈到最大元素。

  • 在迴圈內部,使用 not in 運算子檢查迭代器值是否不在給定列表中。

  • 如果它不在給定列表中,則使用 append() 函式將此元素新增到新列表中。

  • 使用 random.choice() 方法從中選擇一個隨機元素(此函式從指定的序列即列表中返回一個隨機元素)。

  • 列印不在列表中的隨機元素。

示例

以下程式使用 random.choice() 函式返回不在輸入列表中的隨機元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =",inputList) # getting the maximum/greatest element from the list max_element = max(inputList) # Taking an empty list to store elements that are not in the list newList = [] # Looping from 0 to max element using the for loop for i in range(max_element): # Checking whether the number is not in the given list if i not in inputList: # If it is not in the given list then add it to the newList newList.append(i) # Get the random number from the newlist using the choice() function of the random module randomElement = random.choice(newList) # printing the random element which is not in the list print("Random element which is not in the list = ", randomElement)

輸出

執行上述程式將生成以下輸出 -

Input List = [3, 10, 5, 9, 2]
Random element which is not in the list = 8

使用 random.choice() 函式和列表推導式

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 從 0 迴圈到最大元素,並使用 not in 運算子選擇不在列表中的元素,然後使用 random.choice() 方法從中選擇一個隨機元素。

  • 列印不在列表中的隨機元素。

示例

以下程式使用 random.choice() 函式和列表推導式返回不在輸入列表中的隨機元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =",inputList) # getting the maximum/greatest element from the list max_element = max(inputList) # Get all the numbers from 0 to max_element that are not present in the list # Selecting a random element from those elements using the choice() function randomElement = random.choice([e for e in range(max_element) if e not in inputList]) # printing the random element which is not in the list print("The random element which is not in the list = ", randomElement)

輸出

執行上述程式將生成以下輸出 -

Input List = [3, 10, 5, 9, 2]
The random element which is not in the list =  7

使用 random.choice() 和 set() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用 set() 函式(建立集合物件。集合列表將以隨機順序出現,因為專案沒有排序。它刪除所有重複項)和 range() 函式,獲取從輸入列表的最低元素到最高元素的數字集合。從輸入列表的集合中減去此集合以刪除公共元素,然後將其轉換為列表,並使用 random.choice() 函式從中選擇一個隨機元素。

  • 列印不在列表中的隨機元素

示例

以下程式使用 random.choice() 和 set() 函式返回 python 中給定輸入列表中不存在的隨機元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List = ",inputList) # Getting the set of numbers from min element of the input list to the maximum element using set() and range() functions # Subtracting this set from the set of the input list to remove the common elements # Converting this to a list and selecting a random element from this list randomElement = random.choice(list(set(range(min(inputList)+1, max(inputList)))-set(inputList))) # printing the random element which is not in the list print("Random element which is not in the list = ", randomElement)

輸出

執行上述程式將生成以下輸出 -

Input List =  [3, 10, 5, 9, 2]
Random element which is not in the list =  4

使用 random.randrange() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用 while 迴圈透過將條件設定為 True 來無限次執行迴圈。

  • 使用 randrange() 函式(返回指定範圍內的隨機數)獲取 1 到 100 之間的隨機元素。

  • 使用 if 條件語句使用 not 和 in 運算子檢查生成的隨機元素是否不在列表中。

  • 如果條件為真,則使用 break 語句跳出迴圈(這意味著我們找到了一個不在給定列表中的元素)。

  • 列印不在列表中的隨機元素

示例

以下程式使用 random.randrange() 函式返回 python 中給定輸入列表中不存在的隨機元素 -

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =", inputList) # Running the loop infinite times while True: # getting the random element from 1 to 100 randomElement=random.randrange(1, 100) # checking whether the generated random element is not present in the list if randomElement not in inputList: # breaking the loop if the condition is true break # printing the random element which is not in the list print("The random element which is not in the list = ", randomElement)

輸出

執行上述程式將生成以下輸出 -

Input List = [3, 10, 5, 9, 2]
The random element which is not in the list =  96

結論

在這篇文章中,我們學習瞭如何使用 Python 的四種不同的方法來選擇不在給定列表中的隨機元素。我們還學習瞭如何使用 set() 函式刪除重複項。

更新於:2022-10-27

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.