[python] itertools库学习
最近做 cyber-dojo上的题,好几道都要用到排列组合。一开始我还老老实实自己写算法。后来一想,不对呀!python有那么多的库,为啥不用呢?
于是搜了下,发现这个:itertools
使用 help(itertools)可以查看帮助,不过内容多了容易头痛,我们慢慢来先。
用dir看个大概:
>>> dir(itertools)
['__doc__', '__file__', '__name__', '__package__', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'groupby',
'ifilter', 'ifilterfalse', 'imap', 'islice', 'izip', 'izip_longest', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee']
------------------------------默默地查下单词先--------------------------------------
combinations 英 [ kɒmbɪ'neɪʃnz ] 美 [ kɒmbɪ'neɪʃnz ] n.合作( combination的名词复数 ); 密码组合; 联合体; 排列 permutations 英 [ pɜ:mju:'teɪʃnz ] 美 [ pɜmju'teɪʃnz ] n. (一组事物可能的一种)序列,排列,排列中的任一组数字或文字( permutation的名词复数 ) cartesian 英 [ kɑ:ˈti:ziən ] 美 [ kɑrˈtiziən ] adj. 笛卡尔的,笛卡尔哲学的 n. 笛卡尔信徒
>>> def pr(a):
... for e in a:
... print e
- combinations
>>> s = ['A', 'T', 'C', 'G']
>>> com = itertools.combinations(s,1)
>>> pr(com)
('A',)
('T',)
('C',)
('G',)
>>> com = itertools.combinations(s,2)
>>> pr(com)
('A', 'T')
('A', 'C')
('A', 'G')
('T', 'C')
('T', 'G')
('C', 'G')
>>> com = itertools.combinations(s,3)
>>> pr(com)
('A', 'T', 'C')
('A', 'T', 'G')
('A', 'C', 'G')
('T', 'C', 'G')
>>> com = itertools.combinations(s,4)
>>> pr(com)
('A', 'T', 'C', 'G')
- permutations
>>> per = itertools.permutations(s)
>>> pr(per)
('A', 'T', 'C', 'G')
('A', 'T', 'G', 'C')
('A', 'C', 'T', 'G')
('A', 'C', 'G', 'T')
('A', 'G', 'T', 'C')
('A', 'G', 'C', 'T')
('T', 'A', 'C', 'G')
('T', 'A', 'G', 'C')
('T', 'C', 'A', 'G')
('T', 'C', 'G', 'A')
('T', 'G', 'A', 'C')
('T', 'G', 'C', 'A')
('C', 'A', 'T', 'G')
('C', 'A', 'G', 'T')
('C', 'T', 'A', 'G')
('C', 'T', 'G', 'A')
('C', 'G', 'A', 'T')
('C', 'G', 'T', 'A')
('G', 'A', 'T', 'C')
('G', 'A', 'C', 'T')
('G', 'T', 'A', 'C')
('G', 'T', 'C', 'A')
('G', 'C', 'A', 'T')
('G', 'C', 'T', 'A')
>>> per1 = itertools.permutations(s,1)
>>> pr(per1)
('A',)
('T',)
('C',)
('G',)
>>> per2 = itertools.permutations(s,2)
>>> pr(per2)
('A', 'T')
('A', 'C')
('A', 'G')
('T', 'A')
('T', 'C')
('T', 'G')
('C', 'A')
('C', 'T')
('C', 'G')
('G', 'A')
('G', 'T')
('G', 'C')
>>> per3 = itertools.permutations(s,3)
>>> pr(per3)
('A', 'T', 'C')
('A', 'T', 'G')
('A', 'C', 'T')
('A', 'C', 'G')
('A', 'G', 'T')
('A', 'G', 'C')
('T', 'A', 'C')
('T', 'A', 'G')
('T', 'C', 'A')
('T', 'C', 'G')
('T', 'G', 'A')
('T', 'G', 'C')
('C', 'A', 'T')
('C', 'A', 'G')
('C', 'T', 'A')
('C', 'T', 'G')
('C', 'G', 'A')
('C', 'G', 'T')
('G', 'A', 'T')
('G', 'A', 'C')
('G', 'T', 'A')
('G', 'T', 'C')
('G', 'C', 'A')
('G', 'C', 'T')
[python] itertools库学习的更多相关文章
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- python标准库学习-SimpleHTTPServer
这是一个专题 记录学习python标准库的笔记及心得 简单http服务 SimpleHTTPServer 使用 python -m SimpleHTTPServer 默认启动8000端口 源码: &q ...
- python requests库学习
Python 第三方 http 库-Requests 学习 安装 Requests 1.通过pip安装 $ pip install requests 2.或者,下载代码后安装: $ git clone ...
- Python turtle库学习笔记
1.简介 Python的turtle库的易操作,对初学者十分友好.对于初学者来说,刚学编程没多久可以写出许多有趣的可视化东西,这是对学习编程极大的鼓舞,可以树立对编程学习的信心.当然turtle本身也 ...
- python requests库学习笔记(下)
1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions #引入exc ...
- python中库学习
一.numpy NumPy的主要对象是同种元素的多维数组.这是一个所有的元素都是一种类型.通过一个正整数元组索引的元素表格(通常是元素是数字).在NumPy中维度(dimensions)叫做轴(axe ...
- Python标准库学习之zipfile模块
ZipFile模块里有两个非常重要的class, 分别是 ZipFile和ZipInfo. ZipFile是主要的类,用来创建和读取zip文件,而ZipInfo是存储的zip文件的每个文件的信息的. ...
- Python PIL库学习笔记
1.PIL简介 Python Imaging Library(缩写为PIL)(在新的版本中被称为Pillow)是Python编程语言的开源库,它增加了对打开,操作和保存许多不同图像文件格式的支持.它适 ...
- python标准库学习-ftplib
源码: """An FTP client class and some helper functions. Based on RFC 959: File Transfer ...
随机推荐
- 【bzoj1232】[Usaco2008Nov]安慰奶牛cheer(最小生成树)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1232 这道题要保留的道路肯定是原图的一棵生成树,因为要保留n-1条边,且使删边后的图连 ...
- springcloud-Api网关服务Zuul
springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir 1.由来: 如果我的微服务中有很多个独立服务都要对 ...
- spring boot 基础学习
构建微服务:Spring boot 入门篇 http://www.cnblogs.com/ityouknow/p/5662753.html SpringBoot入门系列:第一篇 Hello World ...
- QT (QSS) 编程, QSS语法概述。。setstylesheet
http://www.cnblogs.com/davesla/archive/2011/01/30/1947928.html 转载] QT皮肤(QSS)编程 借用css 的灵感, Qt也支持Qt自己的 ...
- ASP.NET上传文件,已经上传的大小保存在session中,在另一个页面中读取session的值不行
想自己做个ASP.NET上传文件时显示进度条的, 按照自己的想法,其实也就是显示每次已经上传的字节,从网上找到一个方法是能够把文件变成流以后再慢慢写入的,我在那个循环写入的时候每循环一次都把已经上传的 ...
- bootstrap参考网站
http://www.chuntao.org.cn/http://www.dianxiaohuo.com/
- js判断是pc还是移动端
//判断pc还是移动端 var isM = function () { var ua = navigator.userAgent; /* navigator.userAgent 浏览器发送的用户代理标 ...
- release与debug的区别
http://www.cnblogs.com/JemBai/archive/2009/01/13/1374805.html
- Django-03
知识预览 分页器(paginator) COOKIE 与 SESSION Django的用户认证 FORM 回到顶部 分页器(paginator) 分页器的使用 1 2 3 4 5 6 7 8 9 1 ...
- mysql分组取topn
本文来自 http://www.jb51.net/article/31590.htm 有些语句sql top n 是sqlserver语法 --按某一字段分组取最大(小)值所在行的数据 代码如下: ...