[英]How to filter only unique pairs in permutations, python?
from itertools import permutations
list0 = [1, 2, 3]
for el1, el2 in permutations(list0, r=2):
print(el1, el2)
Output: Output:
1 2
1 3
2 1
2 3
3 1
3 2
This output contains all possible permutations, I need unique combinations of numbers, how should I filter them, maybe I should use another code.这个 output 包含所有可能的排列,我需要唯一的数字组合,我应该如何过滤它们,也许我应该使用另一个代码。 I need
我需要
1 2
1 3
2 3
You can use combinations
instead of permutations
to achieve that您可以使用
combinations
而不是permutations
来实现这一点
from itertools import combinations
list0 = [1, 2, 3]
for combination in combinations(list0, 2):
print(combination)
Are you probably looking for combinations您可能正在寻找组合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.