【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 ...
随机推荐
- vue todolist 1.0
<template> <div id="app"> <input type="text" v-model='todo' /> ...
- kappa系数
kappa计算结果为-1~1,通常kappa是落在 0~1 间,可分为五组来表示不同级别的一致性: 0.0~0.20 极低的一致性(slight) 0.21~0.40 一般的一致性(fair) 0.4 ...
- 在线学习和在线凸优化(online learning and online convex optimization)—凸化方法4
一些在线预测问题可以转化到在线凸优化框架中.下面介绍两种凸化技术: 一些在线预测问题似乎不适合在线凸优化框架.例如,在线分类问题中,预测域(predictions domain)或损失函数不是凸的.我 ...
- java运行原理剖析
java是一种编译型语言,我们开发好的代码是不能够直接运行的,需要我们程序员进行编译之后,将字节点文件载入jvm虚拟之后,才可以运行操作 其原理是 java源代码-------编译-------> ...
- 用python进行桌面程序开发
Python是一种面向对象.直译式计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定.这种语言具有非常简捷而清晰的语法特点,适合完成各种高层任务,几乎可以在所 ...
- 第11章 拾遗5:IPv6和IPv4共存技术(1)_双栈技术和6to4隧道技术
6. IPv6和IPv4共存技术 6.1 双栈技术 (1)双协议主机的协议结构 (2)双协议栈示意图 ①双协议主机在通信时首先通过支持双协议的DNS服务器查询与目的主机名对应的IP地址. ②再根据指定 ...
- (转)C# WebApi 异常处理解决方案
原文地址:http://www.cnblogs.com/landeanfen/p/5363846.html 一.使用异常筛选器捕获所有异常 我们知道,一般情况下,WebApi作为服务使用,每次客户端发 ...
- angularjs,Jsonp跨域访问页面
angularjs1.6.8版本跨域 <!DOCTYPE html> <html ng-app="test"> <head> <meta ...
- SQLServer树查询
感觉这个CTE递归查询蛮好用的,先举个例子: use City; go create table Tree ( ID int identity(1,1) primary key not null, N ...
- ROS 进阶学习笔记(13) - Combine Subscriber and Publisher in Python, ROS
Combine Subscriber and Publisher in Python, ROS This article will describe an example of Combining S ...