[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 ...
随机推荐
- awk之腾迅面试题1
1.题目如下: 3 5 6 72 3 1 04 5 6 92 3 4 42 2 1 04 5 0 9假如把2列和3列的值作为新的第5列,第5列的平均值为avg5,求第5列中大于avg5的行数. aw ...
- SG函数略解
由于笔者太懒,懒得把原来的markdown改成MCE,所以有很多奇怪的地方请谅解. 先说nim游戏. 大意:有n堆石子,两个人轮流取,每个人每次从任意一堆取任意个,直到一个人无法取了为止.问对于石子的 ...
- Kafka详解六:Kafka如何通过源码实现监控
问题导读: 1.kafka的消费者组的消费偏移存储,kafka支持两个版本? 2.ConsumerOffsetChecker类的作用是什么? 3.Kafka如何通过源码实现 ...
- tyvj 1884 [NOIP2000T4]方格取数 || codevs 1043 dp
P1884 [NOIP2000T4]方格取数 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 [noip2000T4]方格取数 描述 设有N*N的方格图(N& ...
- 关于synchronized关键字
1.synchronized关键字的作用域有二种: 1)是某个对象实例内,synchronized aMethod(){}可以防止多个线程同时访问这个对象的synchronized方法(如果一个对象有 ...
- Overlay网络与物理网络的关系
编者按:无论是云计算还是SDN都把注意力集中在数据中心网络的建设上,各种解决方案层出不穷,其中以VMware为代表的软件厂商提出Overlay网络方案后,为数据中心网络的发展提出了新的思路.那么Ove ...
- HTML5 拖放---drag和drop
拖放四步走:第一步:设置元素可拖放,即把 draggable属性设置为 true: 例:<div id="div" draggable="true"&g ...
- Deep Learning(Ian Goodfellow) — Chapter1 Introduction
Deep Learning是大神Ian GoodFellow, Yoshua Bengio 和 Aaron Courville合著的深度学习的武功秘籍,涵盖深度学习各个领域,从基础到前沿研究.因为封面 ...
- 论文笔记 — Learning to Compare Image Patches via Convolutional Neural Networks
论文: 引入论文中的一句话来说明对比图像patches的重要性,“Comparing patches across images is probably one of the most fundame ...
- android之Uri的常用几个例子
显示网页: 1. Uri uri = Uri.parse("http://www.google.com"); 2. Intent it = new Intent(Inten ...