Python 交换数字时如何创建列表列表

Python 交换数字时如何创建列表列表,python,python-3.x,Python,Python 3.x,假设我有一个数字列表 L1 = [1,2,3] 我希望能够从这个列表中生成许多列表,同时交换数字 L1 = [1,2,3] L2 = [2,1,3] L3 = [3,2,1] L4 = [1,3,2] 最好的方法是什么 from itertools import permutations print(list(permutations(L1))) 同时给你一张你想要的清单 from itertools import permutations L1 = [1,2,3] for p in

假设我有一个数字列表

L1 = [1,2,3]
我希望能够从这个列表中生成许多列表,同时交换数字

L1 = [1,2,3] 
L2 = [2,1,3]
L3 = [3,2,1]
L4 = [1,3,2]
最好的方法是什么

from itertools import permutations

print(list(permutations(L1)))
同时给你一张你想要的清单

from itertools import permutations
L1 = [1,2,3]
for p in permutations(L1):
    print list(p)

output:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
同时给你一张你想要的清单

from itertools import permutations
L1 = [1,2,3]
for p in permutations(L1):
    print list(p)

output:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]