1. 参考

几个有用的python函数 (笛卡尔积, 排列, 组合)

9.7. itertools — Functions creating iterators for efficient looping

2. 代码

 # 有序排列permutations A。
# 不放回抽球两次,r参数默认为len('abc')
>>> for i in itertools.permutations('abc',2):
... print(i)
...
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
# 无序组合combinations C。
# 不放回抽球两次,r必选
>>> for i in itertools.combinations('abc',2):
... print(i)
...
('a', 'b')
('a', 'c')
('b', 'c') # 笛卡尔积
# 放回抽球,默认repeat=1
# product(A, B) returns the same as: ((x,y) for x in A for y in B).
# repeat=2相当于for i in itertools.product('abc','abc')
>>> for i in itertools.product('abc',repeat=2):
... print(i)
...
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
# 放回抽球,r必选,相当于product再去掉非自定义字典序'CBA'顺序的
>>> for i in itertools.combinations_with_replacement('CBA', 2):
... print(i)
...
('C', 'C')
('C', 'B')
('C', 'A')
('B', 'B')
('B', 'A')
('A', 'A')

python排列组合之itertools模块的更多相关文章

  1. Python排列组合问题

    1.字符串的全排列 问题描述:打印出原字符串中所有字符的所有排列.——将输入字符串中的每个字符作为一个不同的字符看待,即使它们是重复的,如'aaa'应打印6次. Python可以用生成器解决: def ...

  2. Python排列组合实验

    import itertools 排列: 4个数内选2个 >>> print list(itertools.permutations([1,2,3,4],2)) [(1, 2), ( ...

  3. python 排列组合

    笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...

  4. Python排列组合

    product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...

  5. python itertools模块练习

    参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...

  6. Python实现排列组合

    # -*- coding: utf-8 -*-"""Created on Sat Jun 30 11:49:56 2018 @author: zhen"&quo ...

  7. 高效的 itertools 模块(转)

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

  8. python itertools模块实现排列组合

    转自:https://blog.csdn.net/specter11235/article/details/71189486 一.笛卡尔积:itertools.product(*iterables[, ...

  9. 【Python】排列组合itertools & 集合set

    ■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...

随机推荐

  1. Ubuntu16下apache2安装ssl阿里云证书

    1.用下面的命令确保ssl模块已经加载进apache: a2enmod ssl 如果你看到了“Module ssl already enabled”这样的信息就说明你成功了,如果你看到了“Enabli ...

  2. CodeForces 931C Laboratory Work 水题,构造

    *这种题好像不用写题解... 题意: 一个人要改动别人的实验记录,实验记录记录是一个集合 实验记录本身满足:$max(X)-min(X)<=2$ 改动结果要求: 1.新的集合平均值和之前的一样 ...

  3. Tour HDU - 3488 有向环最小权值覆盖 费用流

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...

  4. planning深度剖析

    planning深度剖析 结合find命令过滤目录及文件名后缀: find /home/hadoop/nisj/automationDemand/ -type f -name '*.py'|xargs ...

  5. exiting pxe rom 无法启动

    背景 我这是给人装了多少次机器了,上千次不敢说,几百次是有了.有个奇怪现象,为什么每次总有新的问题呢,极少能一次成功的.除了让我涨了见识,没想到其他的用处.程序员修电脑,搞笑吧,还有找我修洗衣机的,说 ...

  6. 031_keepalive+nginx保证nginx高可用

    一. yum -y install keepalived keepalived配置: keepalived.conf: vrrp_instance proxy { state BACKUP inter ...

  7. Git如何克隆远程仓库

    1.首先选择一个合适的地方创建一个空目录 mkdir learngit     2.通过git Init命令把这个目录变成git可以管理的仓库,瞬间git就把仓库建好了 3.将编写的文件放到 lear ...

  8. Python split()

    split翻译为分裂.  split()就是将一个字符串分裂成多个字符串组成的列表. split()当不带参数时以空格进行分割,当代参数时,以该参数进行分割. //---当不带参数时 example: ...

  9. swoole深入学习 2. tcp Server和tcp Client

    这节来学习Swoole最基础的Server和Client.会通过创建一个tcp Server来讲解. server <?php class Server { private $serv; pub ...

  10. 用ngif 多次判断 Expression has changed after it was checked

    昨天遇到一个问题 ,用ng Expression has changed after it was checked 查了一下说在angular2中,这个错误只会在dev开发模式下出现,在pro发布版本 ...