在两个列表之间随机打乱元素

Ser*_*ado 0 python random shuffle list

我有两个清单:

list_1 = [a, b, c, d, e]

list_2 = [f, g, h, i]
Run Code Online (Sandbox Code Playgroud)

我想要做的是一种在列表之间随机打乱两个元素的方法,例如:

shuffle_two_lists(list_1, list_2 )

list_1 = [g, b, c, d, e]

list_2 = [f, a, h, i]
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

cs9*_*s95 5

你可以把它们组合起来,shuffle然后分开。

import random; random.seed(3.14)

m = len(list_1)
list_3 = list_1 + list_2
random.shuffle(list_3)
list_1, list_2  = list_3[:m], list_3[m:]
Run Code Online (Sandbox Code Playgroud)
print(list_1)
['h', 'a', 'g', 'f', 'b']

print(list_2)
['i', 'e', 'c', 'd']
Run Code Online (Sandbox Code Playgroud)