python全排列组合
发布时间: 2024-01-08 05:57:04
⑴ 如何用Python列出N个数字的所有排列组合
>> from itertools import combinations, permutations
>> permutations([1, 2, 3], 2)
<itertools.permutations at 0x7febfd880fc0>
# 可迭代对象
>> list(permutations([1, 2, 3], 2)) #排列
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>> list(combinations([1, 2, 3], 2)) #组合
[(1, 2), (1, 3), (2, 3)]
热点内容