Python中实现排列组合,从M个元素中有序或者无序选取N个元素的集合。
import itertools
'''
无序排列
combinations(M个数的集合,选取N个数为一组)
'''
c = list(itertools.combinations([1, 2, 3, 4], 2))
print(c)
'''
有序排列
permutations(M个数的集合,选取N个数为一组)
'''
p = list(itertools.permutations([1, 2, 3, 4], 2))
print(p)
'''
测试结果:
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
'''