从3个数字生成所有组合

Abh*_*kur -4 python

要在python中生成3个(或更多)数字的所有排列,可以使用itertools.permutations.如何分批生成两个组合.

对于前者 :输入数字列表:[1, 2, 3] 和输出:[1,2], [2,3], [1,3]

iCo*_*dez 8

用途itertools.combinations:

>>> from itertools import combinations
>>> lst = [1, 2, 3]
>>> list(combinations(lst, 2))
[(1, 2), (1, 3), (2, 3)]
>>> [list(x) for x in combinations(lst, 2)]
[[1, 2], [1, 3], [2, 3]]
>>>
Run Code Online (Sandbox Code Playgroud)