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. telnetlib 中各种 read 函数的意义

    基本原理 要明白 telnetlib 中各个 read 函数的意义,首先要了解 telnetlib 的工作原理. telnetlib 首先通过 socket 连接从网络接收数据,把数据存储到自己的 r ...

  2. 题解-ZeroJudge-c686 高斯符號

    Problem ZeroJudge Solution 考慮到\(\lfloor \frac {km}n\rfloor\)等同於\(km\)整除\(n\),換種表示方法就是\(km\)減去\(km\)模 ...

  3. 神经网络中的偏置项b到底是什么?

    原文地址:https://blog.csdn.net/Uwr44UOuQcNsUQb60zk2/article/details/81074408 前言        很多人不明白为什么要在神经网络.逻 ...

  4. MybatisGenerator生成的mapper 少了识别主键的方法 byPrimaryKey()

    生成的文件缺少红线标注的类似方法 添加 <property name="useInformationSchema" value="true"/>即可 ...

  5. 基于官方mysql镜像构建自己的mysql镜像

    参考文章:https://www.jb51.net/article/115422.htm搭建步骤 1.首先创建Dckerfile: 1 2 3 4 5 6 7 8 9 10 11 12 FROM my ...

  6. Codeforces Educational Codeforces Round 57 题解

    传送门 Div 2的比赛,前四题还有那么多人过,应该是SB题,就不讲了. 这场比赛一堆计数题,很舒服.(虽然我没打) E. The Top Scorer 其实这题也不难,不知道为什么这么少人过. 考虑 ...

  7. Oracle存储过程中跳出循环的写法

    注:本文来源于: <  Oracle存储过程中跳出循环的写法   > Oracle存储过程中跳出循环的写法 记录exit和return的用法 1:exit用来跳出循环 loop IF V_ ...

  8. D3.js 使用缩放zoom时节点无法拖动,只能整体移动的问题

    .on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); }) https://stackover ...

  9. css样式之补充。。。

    css常用的一些属性: 1.去掉下划线 :text-decoration:none ;2.加上下划线: text-decoration: underline; 3.调整文本和图片的位置(也就是设置元素 ...

  10. hdu4003

    /*依赖背包的通常做法就是对于每个结点,先处理处其所有子节点的dp,然后对于当前结点进行分组背包dp即可 还是依赖背包问题,dp[i][j]表示结点i的子树用了j个机器人的搜索代价 边界条件,如果某个 ...