【Python】itertools模块

product

例一

product(A, B)函数,返回A、B中的元素的笛卡尔积的元组。听起来有点绕,先看代码吧:

>>> import itertools
>>> itertools.product([1,2,3],[100,200])
<itertools.product object at 0x7f3e6dd7bc80>
>>> for item in itertools.product([1,2,3],[100,200]):
...     print item
...
(1, 100)
(1, 200)
(2, 100)
(2, 200)
(3, 100)
(3, 200)

product(list1, list2) 依次取出list1中的每1个元素,与list2中的每1个元素,组成元组, 
然后,将所有的元组组成一个列表,返回。

例二

for item in product(range(3), repeat=2):
    print(item)
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)

参考

https://blog.csdn.net/qq_33528613/article/details/79365291 
http://www.wklken.me/posts/2013/08/20/python-extra-itertools.html#itertoolsproductiterables-repeat(超级全)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值