标准语法

itertools.product(*iterables[, repeat])

含义

Cartesian product of input iterables.

Roughly equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B).

The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product tuples are emitted in sorted order.

To compute the product of an iterable with itself, specify the number of repetitions with the optional repeat keyword argument. For example, product(A, repeat=4) means the same as product(A, A, A, A).

大致的意思呢就是返回可迭代对象的笛卡尔积,可选参数repeat指定重复次数。若要计算可迭代对象与自身的乘积,请使用可选的repeat关键字参数指定重复次数。例如,乘积(A, repeat=4)与乘积(A, A, A, A)相同。

代码

def product(*args, **kwds):

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

# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

pools = map(tuple, args) * kwds.get('repeat', 1)

result = [[]]

for pool in pools:

result = [x+[y] for x in result for y in pool]

for prod in result:

yield tuple(prod)

算法示例

标签:product,repeat,乘积,python,itertools,result,iterables

来源: https://blog.csdn.net/weixin_51656605/article/details/112913723

python中itertools的用法,【python日用】itertools.product用法相关推荐

  1. python中怎么做分组问题_详解Python中的分组函数groupby和itertools)

    具体代码如下所示: from operator import itemgetter #itemgetter用来去dict中的key,省去了使用lambda函数 from itertools impor ...

  2. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下  

  3. python open 打开是什么类型的文件-详解Python中open()函数指定文件打开方式的用法...

    文件打开方式 当我们用open()函数去打开文件的时候,有好几种打开的模式. 'r'->只读 'w'->只写,文件已存在则清空,不存在则创建. 'a'->追加,写到文件末尾 'b'- ...

  4. python中while。。。。else的用法

    python中while....else的用法 python中的while后面的else的作用是指,当while循环正常执行,中间没有break的时候,会执else后面的语句. 但是如果while语句 ...

  5. python整段代码注释-Python中注释(多行注释和单行注释)的用法实例

    Python中注释(多行注释和单行注释)的用法实例 发布时间:2020-09-30 23:18:32 来源:脚本之家 阅读:97 前言 学会向程序中添加必要的注释,也是很重要的.注释不仅可以用来解释程 ...

  6. python读取枚举_在python中枚举(enumerate in python)

    在python中枚举(enumerate in python) 说, term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4 ...

  7. python中八进制_在Python中以八进制格式输入数字

    python中八进制 Syntax to convert octal value to an integer (decimal format), 将八进制值转换为整数(十进制格式)的语法, int(o ...

  8. python 中^是什么意思,python 中%是什么意思

    python中^是什么意思 在Python" // "表示整数除法.Python其它表达式:Python的表达式写法与C/C++类似.只是在某些写法有所差别.主要的算术运算符与C/ ...

  9. python使用符号#表示单行注释-Python中注释(多行注释和单行注释)的用法实例...

    前言 学会向程序中添加必要的注释,也是很重要的.注释不仅可以用来解释程序某些部分的作用和功能(用自然语言描述代码的功能),在必要时,还可以将代码临时移除,是调试程序的好帮手. 当然,添加注释的最大作用 ...

  10. python中range 函数_pythonrange,range函数的用法

    当range()函数内填入第三个参数时,第三个参数表示百步长.函数语法:range(start,stop[,step])参数说明:start:计数从度start开始.默认是从0开始.例知如道range ...

最新文章

  1. 没有绝对安全的系统!激光瞄准二极管,25米外从被物理隔绝的计算机中窃取数据...
  2. RGB HSV HLS三种色彩模式转换(C语言实现)
  3. python利器怎么用-Python爬虫利器二之Beautiful Soup的用法
  4. 谷歌发布企业版TensorFlow,或提供收费服务?
  5. 论文阅读:Joint Discriminative and Generative Learning for Person Re-identification
  6. Linux下使用iotop检测磁盘io使用情况
  7. [渝粤教育] 西南科技大学 土木工程材料 在线考试复习资料
  8. 中国象棋博弈源代码Android,中国象棋源代码
  9. 2015计算机二级office真题,2015年计算机二级office题库及答案
  10. 软件测试工程师必备技能之Java基础
  11. Django models模块字段注释
  12. 项目经理成功的五个关键因素
  13. Android 学习
  14. Vibosoft ePub Converter(ePub转换器)v2.1.24官方版
  15. Windows 中 TCP 端口 139 和 445 的使用
  16. GO 语言 Web 开发实战一
  17. 数学辅助软件Geogebra工具介绍
  18. 十六进制和字符串之间的转换
  19. JS 实现点击图片查看大图(含注释)
  20. python(分割txt为多个txt)

热门文章

  1. 情景喜剧消亡史:人人都爱,没人敢拍
  2. 哪吒汽车在售车型涨价3000-5000元
  3. iPhone 14仅两款Pro版搭载A16芯片 另外两款继续使用A15
  4. 世纪华通与华为签署合作协议,加快推进绿色数据中心建设
  5. OPPO Find X5/Pro搭载一体化流线设计,采用双芯片战略
  6. 亿纬锂能:拟参与竞拍兴华锂盐35.2857%股权 挂牌价1.44亿元
  7. B站升级HDR10真彩画质,开启4K+120帧+HDR创作新时代
  8. 特斯拉Model Y在6月份销售7500辆 是Model X两倍多
  9. 魅族17系列渲染图曝光:“防爆盾”后盖引网友无限吐槽
  10. 蔚来汽车:ES6和ES8首任车主可享受终生免费换电服务