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)]
熱點內容