python itertools模块

import itertools

1.有序排列

a=itertools.permutations([1,2,3,4],2)
print a
#结果: 
<itertools.permutations object at 0x0000000001FD8F10>
print list(a)
#结果:
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

2.无序组合

a=itertools.combinations([1,2,3,4],2)
print a
print list(a)
#结果:
<itertools.combinations object at 0x00000000021833B8>
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

你可能感兴趣的:(python)