参考: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的更多相关文章

  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. Oracle 在SQL语句中如何获取系统当前时间并进行操作

    select sysdate from dual;select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; select to_char(s ...

  2. zabbix_agentd.conf配置文件详解

    Alias key的别名,例如 Alias=ttlsa.userid:vfs.file.regexp[/etc/passwd,^ttlsa:.:([0-9]+),,,,\1], 或者ttlsa的用户I ...

  3. 常用docker镜像

    oracle12c: mkdir -p /path/to/oradata docker run --name oracle12c \ -p 1521:1521 -p 5500:5500 \ -v /p ...

  4. vue808

    自定义键盘信息:    Vue.directive('on').keyCodes.ctrl=17;    Vue.directive('on').keyCodes.myenter=13; 数据深度监听 ...

  5. scala使用hbase新api

    import org.apache.hadoop.hbase.{HTableDescriptor,HColumnDescriptor,HBaseConfiguration,TableName} imp ...

  6. 通过mysqlclient操作MySQL数据库

    一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...

  7. 2018北美部分CS项目学费

    yearly cost from official website USC 城里 24credit about 49k + cost of room&food BU 城里 NEU 65k 城里 ...

  8. c helloworld

    #include <stdio.h> int main() { int i; printf("%s","hello, world"); } 1.#i ...

  9. 干货 | 100+个NLP数据集大放送,再不愁数据!

    奉上100多个按字母顺序排列的开源自然语言处理文本数据集列表(原始未结构化的文本数据),快去按图索骥下载数据自己研究吧! 数据集 Apache软件基金会公开邮件档案:截止到2011年7月11日全部公开 ...

  10. mySQL InnoDB 的性能问题讨论

    https://ncisoft.iteye.com/blog/34676 https://www.douban.com/note/245895324/ MySQL最为人垢病的缺点就是缺乏事务的支持,M ...