当分为两个子列表时,我需要列表元素的所有可能唯一组合。
例如-如果我有一个列表: [1,2,3] 我想将其分为两个子列表,如下所示:
[1],[2,3]
[1,2],[3]
[2],[1,3]
现在我将如何找出所有这些独特的组合, 而且,这里忽略了元素的顺序。
答案 0 :(得分:0)
您可以使用itertools.permutations()
from itertools import permutations
# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3])
# Print the obtained permutations
for i in list(perm):
print (list(i[:1]),list(i[1:]))
print (list(i[:2]),list(i[2:]))
输出:
[1] [2, 3]
[1, 2] [3]
[1] [3, 2]
[1, 3] [2]
[2] [1, 3]
[2, 1] [3]
[2] [3, 1]
[2, 3] [1]
[3] [1, 2]
[3, 1] [2]
[3] [2, 1]
[3, 2] [1]