>>> import itertools
>>> list(itertools.permutations([1,2,3],2))
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>>> list(itertools.permutations([1,2,3],3))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
>>> set(itertools.permutations([1,2,3],3))
{(1, 3, 2), (3, 2, 1), (2, 1, 3), (3, 1, 2), (1, 2, 3), (2, 3, 1)}
>>> tuple(itertools.permutations([1,2,3],3))
>>> list(itertools.permutations([1,2,3],2))
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>>> list(itertools.permutations([1,2,3],3))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
>>> set(itertools.permutations([1,2,3],3))
{(1, 3, 2), (3, 2, 1), (2, 1, 3), (3, 1, 2), (1, 2, 3), (2, 3, 1)}
>>> tuple(itertools.permutations([1,2,3],3))
((1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1))
>>> set(itertools.permutations('ABC',2))
{('B', 'C'), ('B', 'A'), ('C', 'A'), ('C', 'B'), ('A', 'B'), ('A', 'C')}
>>>
真是个神奇的东西,大大的有用,呵呵
本文介绍了Python标准库itertools中的permutations函数用法,通过实例展示了如何使用该函数生成指定长度的所有可能排列组合,包括整数列表及字符字符串等不同类型的数据。
2943

被折叠的 条评论
为什么被折叠?



