Python product函数介绍

本文介绍了Python的itertools.product函数,展示了如何使用它来实现矩阵的笛卡尔积。例如,product([1,2],[10,20])将产生(1,10),(1,20),(2,10),(2,20)这样的组合。此外,还解释了repeat参数的用法,如product(range(2),repeat=3)相当于product([0,1],[0,1],[0,1]),生成所有可能的三元组。
摘要由CSDN通过智能技术生成

通过 from itertools import product 引入product函数。

Product函数可以实现对矩阵做笛卡尔积

import itertools
for item in itertools.product([1,2],[10,20]):
    print(item)

'''
(1, 10)
(1, 20)
(2, 10)
(2, 20)
'''

iterables是可迭代对象,repeat指定iterable重复几次,即:

product(A,repeat=3)等价于product(A,A,A)

 product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy

 product(range(2), repeat=3)  等价于product([0,1],[0,1],[0,1]) 

--> 000 001 010 011 100 101 110 111

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值