如何在 Python 中隨機打亂物件列表?


在本文中,我們將向您展示如何在 Python 中隨機打亂物件列表。以下是完成此任務的各種方法

  • 使用 random.shuffle() 函式

  • 使用 random.sample() 函式

  • 使用 Fisher-Yates 洗牌演算法

  • 使用 random.randint() 和 pop() 函式

假設我們已經獲取了一個包含一些元素的列表。我們將使用上面指定的不同方法隨機打亂列表的元素。

使用 random.shuffle() 函式

random 模組中的 **shuffle()** 方法用於打亂列表。它接收一個序列(例如列表)並重新組織專案的順序。

此 shuffle() 方法會 **更改** 原始列表,它不會返回新列表。在此過程中,列表的排序會丟失,這是此方法的唯一缺點。

語法

random.shuffle(sequence, function)

引數

  • **sequence** - 任何序列,例如列表、元組等。

  • **function(可選)** - 返回 0.0 到 1.0 之間的值的函式名稱。如果未指定,將使用 random() 函式。

演算法(步驟)

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

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

  • 建立一個變數來儲存輸入列表。

  • 列印輸入列表。

  • 使用 **random.shuffle()** 函式透過將列表作為引數傳遞給它來隨機打亂所有列表元素。

  • 列印結果的隨機列表。

示例

以下程式使用 random.shuffle() 函式返回隨機列表。

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print ("Input list: ", inputList) # shuffling the list of elements using the random module shuffle function random.shuffle(inputList) # Printing the shuffled list print ("Shuffled list: ", inputList)

輸出

執行上述程式後,將生成以下輸出。

Input list: [3, 10, 5, 9, 2]
Shuffled list: [9, 3, 10, 2, 5]

使用 random.sample() 函式

Python 中的 **random.sample()** 方法返回一個新的隨機列表。原始列表保持不變。

random.sample() 方法返回一個列表,該列表包含從序列中隨機選擇的數量的元素。

語法

random.sample(sequence, k)

引數

  • **sequence** - 任何序列,例如列表、元組等

  • **k** - 要返回的元素數量

演算法(步驟)

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

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

  • 使用 **random.sample()** 函式透過傳遞輸入列表和使用 len() 函式(len() 方法返回物件中的專案數)的輸入列表長度作為引數來隨機打亂所有列表元素。

  • 列印結果的隨機列表。

示例

以下程式使用 random.shuffle() 函式返回隨機列表。

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print ("Input list: ", inputList) # shuffling the list elements using the sample function shuffledList = random.sample(inputList, len(inputList)) # Printing the shuffled list print ("Shuffled list: ", shuffledList)

輸出

執行上述程式後,將生成以下輸出。

Input list: [3, 10, 5, 9, 2]
Shuffled list: [2, 5, 9, 10, 3]

使用 Fisher-Yates 洗牌演算法

這是 Python 中一個眾所周知的演算法,用於打亂數字序列。

Fisher-Yates 洗牌演算法

Fisher-Yates 洗牌演算法的時間複雜度為 O(n)。假設我們得到了函式 rand(),它在 O(1) 時間內生成一個隨機數。其思想是從最後一個元素開始,將其與從整個陣列(包括最後一個元素)中隨機選擇的元素交換。考慮從 0 到 n-2 的陣列(大小減少一個),並重復此過程,直到我們到達第一個元素。

演算法(步驟)

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

  • 使用 for 迴圈從列表的末尾遍歷,使用 **len() 函式**(返回物件中的專案數)

  • 使用 random. **randint()** 方法(返回指定範圍內的隨機數)獲取直到當前索引值的隨機索引

  • 將當前索引元素與隨機索引處的元素交換

  • 列印結果的隨機列表。

示例

以下程式使用 Fisher-Yates 洗牌演算法返回隨機列表。

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print ("Input list: ", inputList) # traversing from the end of the list(In reverse order) for p in range(len(inputList)-1, 0, -1): # getting a random index from 0 to the current index q = random.randint(0, p + 1) # Swap the current index element with the element at a random index inputList[p], inputList[q] = inputList[q], inputList[p] # Printing the shuffled list print ("Shuffled list: ", inputList)

輸出

執行上述程式後,將生成以下輸出。

Input list: [3, 10, 5, 9, 2]
Shuffled list: [9, 10, 3, 5, 2]

使用 random.randint() 和 pop() 函式

**random.randint()** - 返回指定範圍內的隨機數

示例

以下程式使用 random.randint() 和 pop() 函式返回隨機列表。

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print("Input list: ", inputList) # getting the list length listLength = len(inputList) # repeating the loop till the length of the list for i in range(listLength): # getting a random index in the range 0 and list Length - 1 randomIndex = random.randint(0, listLength-1) # deleting the element at that corresponding index from the list ele= inputList.pop(randomIndex) # appending the above-deleted element to the input list(adding the element at last) inputList.append(ele) # Printing the shuffled list print ("Shuffled list: ", inputList)

輸出

執行上述程式後,將生成以下輸出。

Input list: [3, 10, 5, 9, 2]
Shuffled list: [10, 2, 3, 5, 9]

結論

在本文中,我們學習了四種不同的 Python 方法來打亂給定的列表。我們還學習了用於打亂列表的 Fisher-Yates 演算法以及如何在 Python 中使用它。

更新於: 2022年10月28日

9K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.