Python中的itertools.product
例子1:import itertools
a = itertools.product([1,2,3],[100,200])
print(a)
for item in itertools.product([1,2,3],[100,200]):
print(item)
输出如下:
<itertools.product object at 0x000001DC4A92B828>
(1, 100)
(1, 200)
(2, 100)
(2, 200)
(3, 100)
(3, 200)
例子2:
vp_pairs = itertools.product(range(5), range(5))
print(vp_pairs)
for item in itertools.product(range(5), range(5)):
print(item)
输出结果:
<itertools.product object at 0x000001FB2773B870>
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 0)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 0)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(4, 0)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
总结:product(A, B)函数,返回A、B中的元素的笛卡尔积的元组。product(list1, list2) 依次取出list1中的每1个元素,与list2中的每1个元素,组成元组,
然后,将所有的元组组成一个列表,返回。
参考文献:https://blog.csdn.net/qq_33528613/article/details/79365291
Python中的itertools.product的更多相关文章
- python中的itertools
在量化数据处理中,经常使用itertools来完成数据的各种排列组合以寻找最优参数 import itertools #1. permutations: 考虑顺序组合元素 items = [1, 2, ...
- python中的itertools模块简单使用
itertools 高效循环下创建循环器的标准库 Infinite itertools,无限迭代器 itertools.count(start=0, step=10) 默认返回一个从0开始,依次+10 ...
- 你真的会python中的for循环吗
for 循环是 Python 中的通用序列迭代器:它可以单步遍历任何有序序列中的元素.for 语句适用于字符串.列表.元组.其他内置可迭代对象和类创建的新对象. for 通常比 while 循环更容易 ...
- 【python 3.6】使用itertools.product进行排列组合
#python 3.6 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import itertools colo ...
- Python小技巧:使用*解包和itertools.product()求笛卡尔积
[问题] 目前有一字符串s = "['a', 'b'],['c', 'd']",想把它分开成为两个列表: list1 = ['a', 'b'] list2 = ['c', 'd'] ...
- Python小技巧:使用*解包和itertools.product()求笛卡尔积(转)
leetcode上做提示时候看到有高人用这个方法解题 [问题] 目前有一字符串s = "['a', 'b'],['c', 'd']",想把它分开成为两个列表: list1 = [' ...
- python中 Lambda,Map,Filter,Itertools,Generator高级函数的用法
Lambda 函数 Lambda 函数是一种比较小的匿名函数--匿名是指它实际上没有函数名. Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda ...
- Python中itertools.groupby分组的使用
Python中itertools.groupby分组的使用 有时候我们需要给一个列表按照某个属性分组,可以借助groupby来实现. 比如:一下列表我想以严重程度给它分组,并求出每组的元素个数. fr ...
- Python标准模块--itertools
1 模块简介 Python提供了itertools模块,可以创建属于自己的迭代器.itertools提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...
随机推荐
- HDU1527 - 取石子游戏【威佐夫博弈】
有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者. ...
- String,StringBuffer,StringBuild的区别
1.三者在执行速度方面的比较:StringBuilder > StringBuffer > String 2.String <(StringBuffer,StringBuild ...
- windows FTP自动下载脚本
新建ftp.dat文件,内容如下 open xxx.xxx.xxx.xxxusernamepasswordcd lcd x:\filesbinaryhashmget *.*bye 新建run.b ...
- C#中的CollectionBase用法
ColectionBase中有List方法,返回的是类本身 class A :CollectionBase { public void add(B b) { List.Add(b); } public ...
- [HTML 5] Atomic Relevant Busy
Together 'aria-live', we can use 'aria-atomic', 'aria-relevant' and 'aria-busy' to give more informa ...
- Android开发之配置adb工具的环境变量
在Android开发中,adb是一个非常好用也非常使用的工具,可是使用的时候假设没有改动环境变量,每一次都须要输入全然路径非常麻烦.解决的方法是在环境变量中加入adb工具的路径. Windows平台 ...
- java 抽象类和接口的差别
语法层面上: 1)抽象类能够提供成员方法的实现细节.而接口中仅仅能存在public abstract 方法. 2)抽象类中的成员变量能够是各种类型的.而接口中的成员变量仅仅能是public st ...
- poj 2683 Ohgas' Fortune 利率计算
水题. 代码: //poj 2683 //sep9 #include <iostream> using namespace std; int main() { int cases; sca ...
- 2015.04.21,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 32
TEASER PREVIEW 如何描述这些人: 很友好.容易相处的人: 不知疲倦的人: 简单.直率..aboveboard([ə'bʌv'bɔ:d] adv. 光明正大地, 率直地 adj. 光明正大 ...
- 0x32 约数
bzoj1053: [HAOI2007]反素数ant bzoj1257: [CQOI2007]余数之和sum Hankson的趣味题 暴力枚举..约数 #include<cstdio> # ...