[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 ...
随机推荐
- js限制输入内容
1,限制输入正整数 onkeyup="value=value.replace(/[^\d]/g,'')" 2,限制输入为数字,包括小数(有瑕疵) onkeyup="val ...
- SBT搭建Spark
http://www.cnblogs.com/yongjian/p/6211007.html http://www.aboutyun.com/thread-8587-1-1.html http://b ...
- springboot项目支持war部署tomcat
最近在学校spring boot 在网络上学校到简单的启动spring boot项目,也搭建好了,但时实际情况我的spring boot项目是要发布到tomcat中的,今天,随意打了个war包发布到t ...
- 表单验证-JS实现
//获取下一个span,可以通过这个对象给状态 function gspan(cobj){ while(true){ if(cobj.nextSibling.nodeName!="SPAN& ...
- Java JDK、Tomcat、Eclipse环境配置
Java 下载地址:http://www.oracle.com/ 根据提示一步一步进行安装,通常安装到C:\Program Files\Java,包含: 环境变量配置: JAVA_HOME:C:\Pr ...
- jsp:<c:redirect> 和<c:param> 标签
redirect 标签使用来进行页面之间的重定向,它和传统 JSP 程序中的<jsp:redirect>标记功能相类似.param 标签是和 redirect 一起使用的,它用来进行参数值 ...
- java:输出流程printStream
// TODO 自动生成的方法存根 //路径文件 File file = new File("F:"+File.separator+"work"+File.se ...
- App Store下载Mac应用失败的解决办法
1. 更换DNS服务器 国内可以用alidns: 223.5.5.5 223.6.6.6 也可以用电信的: 114.114.114.114 国外的可以考虑Google 8.8.8.8 8.8.4.4 ...
- EXCEL对比重复数据
一. EXCEL 突出重复项 1. 选择对应的数据 EXCEL 里选择好数据 2. 选择条件格式 这样就完成了数据重复的突出,可以按条件筛选.选择自己想要的数据
- LeetCode OJ:Combination Sum III(组合之和III)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...