Python|洗牌列表的方法
对数字序列进行洗牌一直是一个有用的实用程序,也是许多公司安置面试中出现的问题。知道不止一种方法来实现这一点总是一个加分项。让我们讨论一些可以实现这一目标的方法。
方法#1: Fisher-Yates shuffle 算法
这是著名的算法之一,主要用于在Python中打乱数字序列。该算法只取较高的索引值,并将其与当前值交换,此过程在循环中重复,直到列表末尾。
Python3
# Python3 code to demonstrate
# shuffle a list
# using Fisher–Yates shuffle Algorithm
import random
# initializing list
test_list = [1, 4, 5, 6, 3]
# Printing original list
print ("The original list is : " + str(test_list))
# using Fisher–Yates shuffle Algorithm
# to shuffle a list
for i in range(len(test_list)-1, 0, -1):
# Pick a random index from 0 to i
j = random.randint(0, i + 1)
# Swap arr[i] with the element at random index
test_list[i], test_list[j] = test_list[j], test_list[i]
# Printing shuffled list
print ("The shuffled list is : " + str(test_list))
Python3
# Python3 code to demonstrate
# shuffle a list
# using random.shuffle()
import random
# initializing list
test_list = [1, 4, 5, 6, 3]
# Printing original list
print ("The original list is : " + str(test_list))
# using random.shuffle()
# to shuffle a list
random.shuffle(test_list)
# Printing shuffled list
print ("The shuffled list is : " + str(test_list))
Python3
# Python3 code to demonstrate
# shuffle a list
# using random.sample()
import random
# initializing list
test_list = [1, 4, 5, 6, 3]
# Printing original list
print ("The original list is : " + str(test_list))
# using random.sample()
# to shuffle a list
res = random.sample(test_list, len(test_list))
# Printing shuffled list
print ("The shuffled list is : " + str(res))
Python3
import random
# Assign array
arr = [1, 2, 3, 4, 5, 6]
# Display original array
print("Original List: ", arr)
# Get length of List
n = len(arr)
#repeat the following for n number of times
for i in range(n):
#select an index randomly
j = random.randint(0, n-1)
#delete the element at that index.
element=arr.pop(j)
#now append that deleted element to the list
arr.append(element)
print("Shuffled List: ",arr)
输出:
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [4, 3, 1, 5, 6]
方法 #2:使用 random.shuffle()
这是最推荐的随机列表方法。 Python在其随机库中提供了这个内置函数,可以就地打乱列表。这样做的缺点是在此过程中丢失了列表排序。对于选择节省时间和忙碌的开发人员很有用。
Python3
# Python3 code to demonstrate
# shuffle a list
# using random.shuffle()
import random
# initializing list
test_list = [1, 4, 5, 6, 3]
# Printing original list
print ("The original list is : " + str(test_list))
# using random.shuffle()
# to shuffle a list
random.shuffle(test_list)
# Printing shuffled list
print ("The shuffled list is : " + str(test_list))
输出:
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [5, 6, 4, 3, 1]
方法 #3:使用 random.sample()
这是一个非常有用的函数,比上面使用的 shuffle 方法更好,它创建一个新的 shuffle 列表并返回它而不是扰乱原始列表的顺序。这在我们需要保留原始列表的情况下很有用。
Python3
# Python3 code to demonstrate
# shuffle a list
# using random.sample()
import random
# initializing list
test_list = [1, 4, 5, 6, 3]
# Printing original list
print ("The original list is : " + str(test_list))
# using random.sample()
# to shuffle a list
res = random.sample(test_list, len(test_list))
# Printing shuffled list
print ("The shuffled list is : " + str(res))
输出:
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [5, 3, 6, 1, 4]
方法四:
在这种方法中,我们随机选择一个索引并将该索引处的元素附加到列表中。
Python3
import random
# Assign array
arr = [1, 2, 3, 4, 5, 6]
# Display original array
print("Original List: ", arr)
# Get length of List
n = len(arr)
#repeat the following for n number of times
for i in range(n):
#select an index randomly
j = random.randint(0, n-1)
#delete the element at that index.
element=arr.pop(j)
#now append that deleted element to the list
arr.append(element)
print("Shuffled List: ",arr)
输出
Original List: [1, 2, 3, 4, 5, 6]
Shuffled List: [4, 5, 3, 2, 6, 1]