如何在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.