【385】itertools 的 product 和 chain 和 accumulate
参考:itertools模块
product
相当于返回两个集合中数据的所有组合可能
Examples from Eric Martin
- from itertools import product
- print(list(product([0, 1], 'abc')))
- print()
- print(list(product(['A', 'B'], ('a', 'b'), range(2))))
- print()
- print(list(product([0, 1], repeat = 2)))
- print()
- print(list(product('ab', repeat = 4)))
- output:
- [(0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c')]
- [('A', 'a', 0), ('A', 'a', 1), ('A', 'b', 0), ('A', 'b', 1), ('B', 'a
- ', 0), ('B', 'a', 1), ('B', 'b', 0), ('B', 'b', 1)]
- [(0, 0), (0, 1), (1, 0), (1, 1)]
- [('a', 'a', 'a', 'a'), ('a', 'a', 'a', 'b'), ('a', 'a', 'b', 'a'), ('
- a', 'a', 'b', 'b'), ('a', 'b', 'a', 'a'), ('a', 'b', 'a', 'b'), ('a',
- 'b', 'b', 'a'), ('a', 'b', 'b', 'b'), ('b', 'a', 'a', 'a'), ('b', 'a
- ', 'a', 'b'), ('b', 'a', 'b', 'a'), ('b', 'a', 'b', 'b'), ('b', 'b',
- 'a', 'a'), ('b', 'b', 'a', 'b'), ('b', 'b', 'b', 'a'), ('b', 'b', 'b'
- , 'b')]
- from itertools import product
- a = (1, 2, 3)
- b = ('A', 'B', 'C')
- c = ('d', 'e', 'f')
- pros = product(a, b, c)
- count = 1
- for elem in pros:
- print(f'{count:02}', "---", elem)
- count+=1
- output:
- 01 --- (1, 'A', 'd')
- 02 --- (1, 'A', 'e')
- 03 --- (1, 'A', 'f')
- 04 --- (1, 'B', 'd')
- 05 --- (1, 'B', 'e')
- 06 --- (1, 'B', 'f')
- 07 --- (1, 'C', 'd')
- 08 --- (1, 'C', 'e')
- 09 --- (1, 'C', 'f')
- 10 --- (2, 'A', 'd')
- 11 --- (2, 'A', 'e')
- 12 --- (2, 'A', 'f')
- 13 --- (2, 'B', 'd')
- 14 --- (2, 'B', 'e')
- 15 --- (2, 'B', 'f')
- 16 --- (2, 'C', 'd')
- 17 --- (2, 'C', 'e')
- 18 --- (2, 'C', 'f')
- 19 --- (3, 'A', 'd')
- 20 --- (3, 'A', 'e')
- 21 --- (3, 'A', 'f')
- 22 --- (3, 'B', 'd')
- 23 --- (3, 'B', 'e')
- 24 --- (3, 'B', 'f')
- 25 --- (3, 'C', 'd')
- 26 --- (3, 'C', 'e')
- 27 --- (3, 'C', 'f')
例子2:二进制数三位数的所有可能
- a = (0, 1)
- b = (0, 1)
- c = (0, 1)
- pros = product(a, b, c)
- count = 1
- for elem in pros:
- print(f'{count:02}', "---", elem)
- count+=1
- output:
- 01 --- (0, 0, 0)
- 02 --- (0, 0, 1)
- 03 --- (0, 1, 0)
- 04 --- (0, 1, 1)
- 05 --- (1, 0, 0)
- 06 --- (1, 0, 1)
- 07 --- (1, 1, 0)
- 08 --- (1, 1, 1)
chain 就是合并成一个 iter
- from itertools import chain
- [e for e in chain([2, 3], {3, 4}, (3,4))]
- output:
- [2, 3, 3, 4, 3, 4]
accumulate 可以实现将可迭代对象进行累加的效果,形成一个新的可迭代对象
- >>> a = accumulate([1, 2, 3, 4])
- >>> [i for i in a]
- [1, 3, 6, 10]
【385】itertools 的 product 和 chain 和 accumulate的更多相关文章
- 【Python】itertools之product函数
[转载]源博客 product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in ...
- Python标准模块--itertools
1 模块简介 Python提供了itertools模块,可以创建属于自己的迭代器.itertools提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...
- Python标准库笔记(10) — itertools模块
itertools 用于更高效地创建迭代器的函数工具. itertools 提供的功能受Clojure,Haskell,APL和SML等函数式编程语言的类似功能的启发.它们的目的是快速有效地使用内存, ...
- python基础===Python 迭代器模块 itertools 简介
本文转自:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...
- python基础=== itertools介绍(转载)
原文链接:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...
- Python3标准库:itertools迭代器函数
1. itertools迭代器函数 itertools包括一组用于处理序列数据集的函数.这个模块提供的函数是受函数式编程语言(如Clojure.Haskell.APL和SML)中类似特性的启发.其目的 ...
- 高效的 itertools 模块(转)
原文地址:http://python.jobbole.com/87380/ 我们知道,迭代器的特点是:惰性求值(Lazy evaluation),即只有当迭代至某个值时,它才会被计算,这个特点使得迭代 ...
- pythonic-迭代器函数-itertools
认识 Python 的itertools模块提供了很多节省内存的高效迭代器, 尤其解决了一些关于数据量太大而导致内存溢出(outofmemory)的场景. 我们平时用的循环绝大多数是这样的. # wh ...
- itertools
0. Python中引入itertools 1. 笛卡尔积: product(iter1, iter2,...,iterN,[repeat=i]) from itertools import prod ...
随机推荐
- java中序列化的简单认识
一.什么是序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长.但在现实应用中,就可 ...
- javascript-typeof篇
isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } isNumber ...
- delphi 获取webbrowser的cookies给Idhttp用
网上方法一:(可获取,但不完全) 引用mshtml; IHTMLDocument(wb1.Document).cooke; 网上方法二:(获取不到!) 引用winnet,使用InternetGetCo ...
- 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 ...
- elasticsearch License 到期后更新步骤
ELK下载安装后有一个月试用期,到期后需要更新License,且每个License有效期为 1 年,License过期前10天里相关log会一直出现,提醒用户更新,支持实时更新无需重启服务. 步骤: ...
- Java平台编写运行Ruby和Python
Java不仅是一门编程语言,还是一个平台,通过JRuby和Jython,我们可以在Java平台上编写和运行Ruby和Python程序.
- centos离线安装docker及其它软件包
桌面版本安装 docker可以通过网络安装,但在内网环境,需要进行离线安装. 执行 uname -r 获取操作系统版本号 根据版本号,到docker.com下载docker的离线安装包: Linux版 ...
- 安装hyperledger fabric V1.0.0-beta
安装文档位置: https://github.com/hyperledger/fabric fabric代码托管地址 https://hyperledger-fabric.readthedoc ...
- [Android] SeekBar---可拖动进度条
SeekBar---可拖动进度条 ()setMax //设置SeekBar最大数值 ()setProgress //设置SeekBar当前数值 ()setSecondaryProgress//设置Se ...
- mysql安装卸载-windows
安装:(注意点) 官网download安装包 choose setup type --> custom 安装路径 detailed configuration developer machin ...