python如何在排列中只过滤唯一对?
输出:python如何在排列中只过滤唯一对?,python,algorithm,loops,Python,Algorithm,Loops,输出: from itertools import permutations list0 = [1, 2, 3] for el1, el2 in permutations(list0, r=2): print(el1, el2) 这个输出包含所有可能的排列,我需要唯一的数字组合,我应该如何过滤它们,也许我应该使用另一个代码。我需要 1 2 1 3 2 1 2 3 3 1 3 2 您可能正在寻找您可以使用组合而不是排列来实现这一点 1 2 1 3 2 3 使用itertools.co
from itertools import permutations
list0 = [1, 2, 3]
for el1, el2 in permutations(list0, r=2):
print(el1, el2)
这个输出包含所有可能的排列,我需要唯一的数字组合,我应该如何过滤它们,也许我应该使用另一个代码。我需要
1 2
1 3
2 1
2 3
3 1
3 2
您可能正在寻找您可以使用
组合
而不是排列
来实现这一点
1 2
1 3
2 3
使用
itertools.compositions
代替itertools.permutations
。
from itertools import combinations
list0 = [1, 2, 3]
for combination in combinations(list0, 2):
print(combination)