标准语法

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 111pools = 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)

算法示例

可参考上一篇文章【python日用】itertools.permutations用法

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

  1. python中itertools的用法,【python日用】itertools.product用法

    标准语法 itertools.product(*iterables[, repeat]) 含义 Cartesian product of input iterables. Roughly equiva ...

  2. Python的itertools.product 方法

    itertools.product:类似于求多个可迭代对象的笛卡尔积. 使用的形式是: itertools.product(*iterables, repeat=1),product(X, repea ...

  3. Python中itertools.product()函数调用

    此次刷题过程中接触到Python中itertools包的product函数调用. product(A,B)用于求可迭代对象A和B的笛卡尔积(后续实例说明),和嵌套的for循环等价, 即product( ...

  4. python中itertools的用法_python中的itertools的使用详解

    今天了解了下python中内置模块itertools的使用,熟悉下,看能不能以后少写几个for,嘿嘿

  5. 【python日用】itertools.permutations用法

    标准语法 itertools.permutations(iterable[, r]) 含义 Return successive r length permutations of elements in ...

  6. Python中的itertools.product

    例子1:import itertools a = itertools.product([1,2,3],[100,200]) print(a) for item in itertools.product ...

  7. python解包操作_Python编程使用*解包和itertools.product()求笛卡尔积的方法

    本文实例讲述了Python编程使用*解包和itertools.product()求笛卡尔积的方法.分享给大家供大家参考,具体如下: [问题] 目前有一字符串s = "['a', 'b'],[ ...

  8. Python Itertools.chain()用法【将一组迭代对象串联起来,形成一个更大的迭代器】

    它是一个需要一系列可迭代对象并返回一个可迭代对象的函数.它将所有可迭代对象组合在一起,并生成一个可迭代对象作为输出. 场景一:  chain()可以把一组迭代对象串联起来,形成一个更大的迭代器: &g ...

  9. python itertools.product_在python中,如何拆分itertools.product分组并在p中迭代

    在python中我使用itertools.product迭代一个字符列表的所有可能的组合,从而产生非常大的结果. 然而,当我查看Windows10任务管理器时,执行此任务的python进程只占用13. ...

最新文章

  1. python英文翻译-Python运算符-局部英文翻译版
  2. Docker宣布企业版支持Windows Server 2019
  3. linux7安装pgsql数据库,CentOS7下PostgreSQL安装
  4. [html] HTML为什么要语义化?语义化有什么好处?
  5. linux free 命令中buffers、cached以及-/+ buffers/cache解析
  6. OpenShift 4 - 获取能访问API服务的用户认证Token
  7. python解图片迷宫_[宜配屋]听图阁
  8. pipeline(管道的连续应用)
  9. java tomcat数据库连接池,tomcat 数据库连接池拿不到连接
  10. 程序员要学点儿理财知识,而不仅仅是代码技巧
  11. 结构化技术写作标准之S1000D——缘起
  12. 卸载wps后,安装office,图标关联失败
  13. C++的引用一,求二次方根
  14. el-element布局控件layout中的el-row和el-col
  15. 开源中文切词工具介绍
  16. 第七章-数据分析-数据透视表的应用
  17. jQueryRotate实现 幸运转盘 Demo
  18. NSComboBox 使用方法
  19. Android Studio 2.3后,找不到Launch Standalone SDK Manager
  20. Python面向对象编程 __init__()方法

热门文章

  1. 不错的培训机构和课程
  2. 安全至上---ASP“动网论坛”漏洞分析
  3. 【6.20校内test】
  4. docker 单节点服务编排部署指南(docker-compose)
  5. 使用Wireshark抓包三次握手四次挥手
  6. 如何查看oracle的DUMP_DIR,Oracle expdp impdp dump参数介绍
  7. 酒店预订系统java_JavaWeb酒店预订系统
  8. Python量化交易学习笔记(18)——放量突破布林线中轨买入策略
  9. 小如雪花的微尘计算机阅读答案,《有趣的“三”》阅读答案
  10. Wannafly挑战赛26:B冥土追魂(模拟?贪心?暴力?)