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


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

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

使用random.choice()函式

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

語法

random.choice(sequence)

引數

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

演算法(步驟)

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

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

  • 建立一個變數來儲存輸入列表,併為其賦一些虛擬值。

  • 使用max()函式(返回iterable中最高值項/最大數字)從輸入列表中獲取最大元素。

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

  • 使用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+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告