【Python】itertools之product函数
【转载】源博客
product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即:
product(A, B) 和 ((x,y) for x in A for y in B)的效果是一样的。
使用形式如下:
itertools.product(*iterables, repeat=1)
iterables 是可迭代对象, repeat指定 iterable 重复几次,即:
product(A,repeat=3)等价于product(A,A,A)
Coding Time:
>>> from itertools import product as product
>>> A = [1, 2, 3]
>>> B = [100, 200, 300]
>>> for item in product(A, B):
... print(item)
...
(1, 100)
(1, 200)
(1, 300)
(2, 100)
(2, 200)
(2, 300)
(3, 100)
(3, 200)
(3, 300)
>>> for item in product(A, repeat=3):
... print(item)
...
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 2, 1)
(1, 2, 2)
(1, 2, 3)
(1, 3, 1)
(1, 3, 2)
(1, 3, 3)
(2, 1, 1)
(2, 1, 2)
(2, 1, 3)
(2, 2, 1)
(2, 2, 2)
(2, 2, 3)
(2, 3, 1)
(2, 3, 2)
(2, 3, 3)
(3, 1, 1)
(3, 1, 2)
(3, 1, 3)
(3, 2, 1)
(3, 2, 2)
(3, 2, 3)
(3, 3, 1)
(3, 3, 2)
(3, 3, 3)
【Python】itertools之product函数的更多相关文章
- Python itertools/内置函数
https://docs.python.org/3.5/library/itertools.html#itertools.starmap // https://docs.python.org/3.5/ ...
- python中 Lambda,Map,Filter,Itertools,Generator高级函数的用法
Lambda 函数 Lambda 函数是一种比较小的匿名函数--匿名是指它实际上没有函数名. Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda ...
- python迭代器与iter()函数实例教程
python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- 实现python中的map函数
假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...
- python学习道路(day4note)(函数,形参实参位置参数匿名参数,匿名函数,高阶函数,镶嵌函数)
1.函数 2种编程方法 关键词面向对象:华山派 --->> 类----->class面向过程:少林派 -->> 过程--->def 函数式编程:逍遥派 --> ...
- python中常用的函数与库一
1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
随机推荐
- XAF中多对多关系 (XPO)
In this lesson, you will learn how to set relationships between business objects. For this purpose, ...
- centos7下编译安装python3.7,且与python2.7.5共存
环境:Centos7.6 x64 一.安装python3.7 下载python源码包: wget https://www.python.org/ftp/python/3.7.4/Python-3.7. ...
- sharepointOnline的外接应用创建
#CODE STARTS HERE $programFiles = [environment]::getfolderpath("programfiles") add-type -P ...
- CSS基本选择器是什么?基本选择器是如何工作
基本选择器介绍 基本选择器又分为六种使用方式:如.通用选择器.标签选择器.类选择器.Id选择器.结合元素选择器.多类选择器. 基本选择器使用说明表. 选择器 语法格式 含义 举例 通用选择器 *{属性 ...
- 高版本 MySQL 导出的脚本到低版本 MySQL 中执行时报错
导入 MySQL 脚本时报错:[ERR] 1273 - Unknown collation: 'utf8mb4_0900_ai_ci'低版本还不支持 utfmb4 这个字符集 解决方法:将 sql 脚 ...
- [转]Outlook VBA自动处理邮件
本文转自:https://blog.csdn.net/hnwyllmm/article/details/44874331 需求描述公司里面每天都会有很多邮件,三分之一都是不需要看的,Outlook的过 ...
- 导入部署 hand
差价导入部署步骤如下: 执行视图,包. 定义消息,验证的时候使用(XXC1003DFM_BI_001 到 XXC1003DFM_BI_007,含中英文). 通用导入设置: 电子表元数据管理,含导入正确 ...
- Python中线程的使用
并发:多个任务同一时间段进行 并行:多个任务同一时刻进行 线程的实现 线程模块 Python通过两个标准库_thread 和threading,提供对线程的支持 , threading对_thread ...
- 系统设计与分析:Alpha版本2成绩汇总
作业要求 1.作业内容 作业具体要求以及评分标准 2.评分细则 •给出开头和团队成员列表(10’) •给出发布地址以及安装手册(20’) •给出测试报告(40’) •给出项目情况总结(30’) * ...
- Requests text乱码
都在推荐用Requests库,而不是Urllib,但是读取网页的时候中文会出现乱码. 分析: r = requests.get(“http://www.baidu.com“) **r.text返回的是 ...