python排列组合之itertools模块
1. 参考
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模块的更多相关文章
- Python排列组合问题
1.字符串的全排列 问题描述:打印出原字符串中所有字符的所有排列.——将输入字符串中的每个字符作为一个不同的字符看待,即使它们是重复的,如'aaa'应打印6次. Python可以用生成器解决: def ...
- Python排列组合实验
import itertools 排列: 4个数内选2个 >>> print list(itertools.permutations([1,2,3,4],2)) [(1, 2), ( ...
- python 排列组合
笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...
- Python排列组合
product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- Python实现排列组合
# -*- coding: utf-8 -*-"""Created on Sat Jun 30 11:49:56 2018 @author: zhen"&quo ...
- 高效的 itertools 模块(转)
原文地址:http://python.jobbole.com/87380/ 我们知道,迭代器的特点是:惰性求值(Lazy evaluation),即只有当迭代至某个值时,它才会被计算,这个特点使得迭代 ...
- python itertools模块实现排列组合
转自:https://blog.csdn.net/specter11235/article/details/71189486 一.笛卡尔积:itertools.product(*iterables[, ...
- 【Python】排列组合itertools & 集合set
■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...
随机推荐
- golang esl api
通过ESL 调取FS的状态,比如show calls : 用golang eventsocket 实现 conn, err := eventsocket.Dial("192.168.5.3 ...
- url分发(二级分发)
from django.shortcuts import HttpResponsedef test(request): return HttpResponse('test') from django. ...
- ueditor使用
<html> <head> <meta charset="UTF-8"> <title></ ...
- python 启动虚拟环境
假设你有两个Python项目-A和B,这两个项目都需要使用同一个第三方模块-tensorflow.如果这两个项目使用相同的tensorflow版本,也许不会有什么问题. 但是,当A和B项目使用不同的t ...
- kafka集群报错
bin/kafka-server-start.sh config/server.properties ,问题来了 : [root@localhost kafka_2.12-0.10.2.0]# Exc ...
- Linux 上的 SQL Server 2017 的安装指南
一:介绍背景 微软在2016年 3 月首次对外宣布了 Linux 版的 SQL Server,并于2017年 7 月发布了首个公开 RC 版.前几日在美国奥兰多召开的微软 Ignite 2017 大会 ...
- oracle 的sqlplus 工具进行翻译的rlwrap 安装教程
一:下载地址: 链接: https://share.weiyun.com/50R5pBb (密码:dQPc) 或者该QQ群下载: 二:该工具的安装步骤: [oracle@localhost ~]$ l ...
- centos 7.3 设置静态IP
注:本文来源:张亮博客 的 <centos 7.3 设置静态IP或ping 报name or service not known> 首先把虚拟机配置为桥接模式,然后开启再你打算修改虚拟机 ...
- pytorch的学习资源
安装:https://github.com/pytorch/pytorch 文档:http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial ...
- 自定义Form组件
一.wtforms源码流程 1.实例化流程分析 # 源码流程 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: meta类读取到cls._ ...