pthon笛卡尔积

导入Itertool函数

使用itertools.product(*iterables, repeat=1)

*iterables指要计算可迭代对象自身的笛卡尔积,将可选参数 repeat 设定为要重复的次数

# 打印出两个列表的笛卡尔积
list3 = [1,2,3]
list4 = ['a','b','c']
list_iter = [x for x in itertools.product(list3,list4)]
print(list_iter)
# list_iter = [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c')]

 

posted @ 2020-12-13 14:06  echo'coding'  阅读(74)  评论(0编辑  收藏  举报