参考:itertools模块

product

相当于返回两个集合中数据的所有组合可能

Examples from Eric Martin

  1. from itertools import product
  2.  
  3. print(list(product([0, 1], 'abc')))
  4. print()
  5. print(list(product(['A', 'B'], ('a', 'b'), range(2))))
  6. print()
  7. print(list(product([0, 1], repeat = 2)))
  8. print()
  9. print(list(product('ab', repeat = 4)))
  10.  
  11. output:
  12. [(0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c')]
  13.  
  14. [('A', 'a', 0), ('A', 'a', 1), ('A', 'b', 0), ('A', 'b', 1), ('B', 'a
  15. ', 0), ('B', 'a', 1), ('B', 'b', 0), ('B', 'b', 1)]
  16.  
  17. [(0, 0), (0, 1), (1, 0), (1, 1)]
  18.  
  19. [('a', 'a', 'a', 'a'), ('a', 'a', 'a', 'b'), ('a', 'a', 'b', 'a'), ('
  20. a', 'a', 'b', 'b'), ('a', 'b', 'a', 'a'), ('a', 'b', 'a', 'b'), ('a',
  21. 'b', 'b', 'a'), ('a', 'b', 'b', 'b'), ('b', 'a', 'a', 'a'), ('b', 'a
  22. ', 'a', 'b'), ('b', 'a', 'b', 'a'), ('b', 'a', 'b', 'b'), ('b', 'b',
  23. 'a', 'a'), ('b', 'b', 'a', 'b'), ('b', 'b', 'b', 'a'), ('b', 'b', 'b'
  24. , 'b')]
  1. from itertools import product
  2. a = (1, 2, 3)
  3. b = ('A', 'B', 'C')
  4. c = ('d', 'e', 'f')
  5. pros = product(a, b, c)
  6. count = 1
  7. for elem in pros:
  8. print(f'{count:02}', "---", elem)
  9. count+=1
  10.  
  11. output:
  12. 01 --- (1, 'A', 'd')
  13. 02 --- (1, 'A', 'e')
  14. 03 --- (1, 'A', 'f')
  15. 04 --- (1, 'B', 'd')
  16. 05 --- (1, 'B', 'e')
  17. 06 --- (1, 'B', 'f')
  18. 07 --- (1, 'C', 'd')
  19. 08 --- (1, 'C', 'e')
  20. 09 --- (1, 'C', 'f')
  21. 10 --- (2, 'A', 'd')
  22. 11 --- (2, 'A', 'e')
  23. 12 --- (2, 'A', 'f')
  24. 13 --- (2, 'B', 'd')
  25. 14 --- (2, 'B', 'e')
  26. 15 --- (2, 'B', 'f')
  27. 16 --- (2, 'C', 'd')
  28. 17 --- (2, 'C', 'e')
  29. 18 --- (2, 'C', 'f')
  30. 19 --- (3, 'A', 'd')
  31. 20 --- (3, 'A', 'e')
  32. 21 --- (3, 'A', 'f')
  33. 22 --- (3, 'B', 'd')
  34. 23 --- (3, 'B', 'e')
  35. 24 --- (3, 'B', 'f')
  36. 25 --- (3, 'C', 'd')
  37. 26 --- (3, 'C', 'e')
  38. 27 --- (3, 'C', 'f')

例子2:二进制数三位数的所有可能

  1. a = (0, 1)
  2. b = (0, 1)
  3. c = (0, 1)
  4. pros = product(a, b, c)
  5. count = 1
  6. for elem in pros:
  7. print(f'{count:02}', "---", elem)
  8. count+=1
  9.  
  10. output:
  11. 01 --- (0, 0, 0)
  12. 02 --- (0, 0, 1)
  13. 03 --- (0, 1, 0)
  14. 04 --- (0, 1, 1)
  15. 05 --- (1, 0, 0)
  16. 06 --- (1, 0, 1)
  17. 07 --- (1, 1, 0)
  18. 08 --- (1, 1, 1)

chain 就是合并成一个 iter

  1. from itertools import chain
  2. [e for e in chain([2, 3], {3, 4}, (3,4))]
  3.  
  4. output:
  5. [2, 3, 3, 4, 3, 4]

accumulate 可以实现将可迭代对象进行累加的效果,形成一个新的可迭代对象

  1. >>> a = accumulate([1, 2, 3, 4])
  2.  
  3. >>> [i for i in a]
  4. [1, 3, 6, 10]

【385】itertools 的 product 和 chain 和 accumulate的更多相关文章

  1. 【Python】itertools之product函数

    [转载]源博客 product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in ...

  2. Python标准模块--itertools

    1 模块简介 Python提供了itertools模块,可以创建属于自己的迭代器.itertools提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...

  3. Python标准库笔记(10) — itertools模块

    itertools 用于更高效地创建迭代器的函数工具. itertools 提供的功能受Clojure,Haskell,APL和SML等函数式编程语言的类似功能的启发.它们的目的是快速有效地使用内存, ...

  4. python基础===Python 迭代器模块 itertools 简介

    本文转自:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...

  5. python基础=== itertools介绍(转载)

    原文链接:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...

  6. Python3标准库:itertools迭代器函数

    1. itertools迭代器函数 itertools包括一组用于处理序列数据集的函数.这个模块提供的函数是受函数式编程语言(如Clojure.Haskell.APL和SML)中类似特性的启发.其目的 ...

  7. 高效的 itertools 模块(转)

    原文地址:http://python.jobbole.com/87380/ 我们知道,迭代器的特点是:惰性求值(Lazy evaluation),即只有当迭代至某个值时,它才会被计算,这个特点使得迭代 ...

  8. pythonic-迭代器函数-itertools

    认识 Python 的itertools模块提供了很多节省内存的高效迭代器, 尤其解决了一些关于数据量太大而导致内存溢出(outofmemory)的场景. 我们平时用的循环绝大多数是这样的. # wh ...

  9. itertools

    0. Python中引入itertools 1. 笛卡尔积: product(iter1, iter2,...,iterN,[repeat=i]) from itertools import prod ...

随机推荐

  1. java中序列化的简单认识

    一.什么是序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长.但在现实应用中,就可 ...

  2. javascript-typeof篇

    isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } isNumber ...

  3. delphi 获取webbrowser的cookies给Idhttp用

    网上方法一:(可获取,但不完全) 引用mshtml; IHTMLDocument(wb1.Document).cooke; 网上方法二:(获取不到!) 引用winnet,使用InternetGetCo ...

  4. Solr——Windows下部署Solr6.6.0至Tomcat8.5.28(一)

    一.window 环境 solr 6.6.3 下载地址 http://archive.apache.org/dist/lucene/solr/ jdk 1.8    tomcat 8.5 本机tomc ...

  5. elasticsearch License 到期后更新步骤

    ELK下载安装后有一个月试用期,到期后需要更新License,且每个License有效期为 1 年,License过期前10天里相关log会一直出现,提醒用户更新,支持实时更新无需重启服务. 步骤: ...

  6. Java平台编写运行Ruby和Python

    Java不仅是一门编程语言,还是一个平台,通过JRuby和Jython,我们可以在Java平台上编写和运行Ruby和Python程序.

  7. centos离线安装docker及其它软件包

    桌面版本安装 docker可以通过网络安装,但在内网环境,需要进行离线安装. 执行 uname -r 获取操作系统版本号 根据版本号,到docker.com下载docker的离线安装包: Linux版 ...

  8. 安装hyperledger fabric V1.0.0-beta

      安装文档位置: https://github.com/hyperledger/fabric   fabric代码托管地址 https://hyperledger-fabric.readthedoc ...

  9. [Android] SeekBar---可拖动进度条

    SeekBar---可拖动进度条 ()setMax //设置SeekBar最大数值 ()setProgress //设置SeekBar当前数值 ()setSecondaryProgress//设置Se ...

  10. mysql安装卸载-windows

     安装:(注意点) 官网download安装包 choose setup type --> custom 安装路径 detailed configuration developer machin ...