笛卡尔积、排列、组合
#网址:https://blog.csdn.net/huangzhiyuan111/article/details/52586316
import itertools
#新知识点
#笛卡尔积
list = [2,3,1,6]
for i in itertools.product(list,repeat=2):
#返回的类型为“tuple”
print(i,end = '')
#结果:(2, 2)(2, 3)(2, 1)(2, 6)(3, 2)(3, 3)(3, 1)(3, 6)(1, 2)(1, 3)(1, 1)(1, 6)(6, 2)(6, 3)(6, 1)(6, 6)
print()
print("***********************************************")
#permutations 排列
for i in itertools.permutations(list,2):
# 返回的类型为“tuple”
print(i,end = '')
#结果:(2, 3)(2, 1)(2, 6)(3, 2)(3, 1)(3, 6)(1, 2)(1, 3)(1, 6)(6, 2)(6, 3)(6, 1)
print()
print("***********************************************")
#combinations 组合,没有重复
for i in itertools.combinations(list,2):
# 返回的类型为“tuple”
print(i,end = '')
#结果:(2, 3)(2, 1)(2, 6)(3, 1)(3, 6)(1, 6)
print()
print("***********************************************")
#combinations_with_replacement 组合,有重复
for i in itertools.combinations_with_replacement(list,2):
# 返回的类型为“tuple”
print(i,end = '')
#结果:(2, 2)(2, 3)(2, 1)(2, 6)(3, 3)(3, 1)(3, 6)(1, 1)(1, 6)(6, 6)
'''
(2, 2)(2, 3)(2, 1)(2, 6)(3, 2)(3, 3)(3, 1)(3, 6)(1, 2)(1, 3)(1, 1)(1, 6)(6, 2)(6, 3)(6, 1)(6, 6)
***********************************************
(2, 3)(2, 1)(2, 6)(3, 2)(3, 1)(3, 6)(1, 2)(1, 3)(1, 6)(6, 2)(6, 3)(6, 1)
***********************************************
(2, 3)(2, 1)(2, 6)(3, 1)(3, 6)(1, 6)
***********************************************
(2, 2)(2, 3)(2, 1)(2, 6)(3, 3)(3, 1)(3, 6)(1, 1)(1, 6)(6, 6)
'''