1.排列组合难题:

必须采用Python的itertools控制模块

import itertools
a=[1,2,3]

#排列,无放回的取,排列(数学公式:A32的含意)
for i in itertools.permutations(a,2):#2是拿2次,a能够是字符串数组或是是目录
    print(i)

'''
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
'''

#组合,无放回的取,组合(数学公式:C32的含意)
# for i in itertools.combinations(a,2):
#     print(i)
'''
(1, 2)
(1, 3)
(2, 3)
'''

#有放回的排列(笛卡尔积)
# for i in itertools.product(a,repeat=2):
#     print(i)

'''
(1, 1)
(1, 2)
(1, 3)
(2, 1)
(2, 2)
(2, 3)
(3, 1)
(3, 2)
(3, 3)
'''

#有放回的组合
# for i in itertools.combinations_with_replacement(a, 2):
#     print(i)
'''
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
'''

 

文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归去快排Seo www.SEOgurublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ▷61910465