python 字符组合排列?(如何用Python列出N个数字的所有排列组合)
更新时间:2021-08-06 11:16:51 • 作者:KRYSTAL •阅读 2487
如何用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)]