Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python如何在排列中只过滤唯一对?_Python_Algorithm_Loops - Fatal编程技术网

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)