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. mongodb 创建更新语法

    创建文档 向MongoDB插入数据,使用insert, 如:db.refactor.insert({"refactor's blog":"http://www.cnblo ...

  2. 题解-Codeforces710F String Set Queries

    咕了好久没更博客,最近得知可以去冬眠营玩耍,还可以搭顺风车回广州过年 (最近做到的比较有意思的题目:bzoj3958.hihocoder1419) Problem Codeforces-710F--洛 ...

  3. windows下java环境变量的配置 javac不是内部或外部命令的问题

    安装配置JAVA JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html . 下载你电脑对应的JDK,下 ...

  4. socket 通信 error:88

    调用函数(setsockopt)来设置 recv buffer 和send  buffer时报错: setsockopt error: Socket operation on non-socket(e ...

  5. codis3.2安装报错dashboard.go:369: [PANIC] call rpc create-proxy to dashboard 127.0.0.1:18080 failed的处理

    codis3.2安装报错dashboard.go:369: [PANIC] call rpc create-proxy to dashboard 127.0.0.1:18080 failed的处理 执 ...

  6. 带jdk15类似的jar配置

    针对部分jar文件名带有jdk15结尾的依赖配置时,需要添加<classifier>标签进行区分 比如针对:json-lib-2.4-jdk15.jar的jar依赖配置 <depen ...

  7. 前端 ----关于DOM的事件操作

    关于DOM的事件操作   一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for ...

  8. 前端 ---- jQuery的ajax

    14-jQuery的ajax   什么是ajax AJAX = 异步的javascript和XML(Asynchronous Javascript and XML) 简言之,在不重载整个网页的情况下, ...

  9. python创建udp服务端和客户端

    1.udp服务端server from socket import * from time import ctime HOST = '' PORT = 8888 BUFSIZ = 1024 ADDR ...

  10. Linux 上的 SQL Server 2017 的安装指南

    一:介绍背景 微软在2016年 3 月首次对外宣布了 Linux 版的 SQL Server,并于2017年 7 月发布了首个公开 RC 版.前几日在美国奥兰多召开的微软 Ignite 2017 大会 ...