itertools.permutations(a, b) 连续返回由a元素生成的长度为b的全排列组合。
例子如下:
import itertools
sum = 0
a=[1, 2, 3, 4, 5]
for i in itertools.permutations(a,2):
print(i)
sum += 1
print(sum)
b = list(itertools.permutations(a, 2))
print(b)
print(len(b))
输出结果:
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(2, 1)
(2, 3)
(2, 4)
(2, 5)
(3, 1)
(3, 2)
(3, 4)
(3, 5)
(4, 1)
(4, 2)
(4, 3)
(4, 5)
(5, 1)
(5, 2)
(5, 3)
(5, 4)
20
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4)]
20