要在python中生成3个(或更多)数字的所有排列,可以使用itertools.permutations.如何分批生成两个组合.
对于前者 :输入数字列表:[1, 2, 3]
和输出:[1,2], [2,3], [1,3]
>>> 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)
| 归档时间: |
|
| 查看次数: |
280 次 |
| 最近记录: |