[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 ...
随机推荐
- 向maven依赖包中添加新的jar包
今天做一个项目测试的时候正好遇到这个问题,查了网上的资料,有两篇写的挺好,两种方法都试了,都可以. 1.个人觉得第一种简单:http://www.360doc.com/content/14/0517/ ...
- CentOS防火墙iptables-config的相关配置参数详解
默认/etc/sysoncifg/iptables-config的配置内容: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...
- Elasticsearch-->Get Started-->Basic concepts
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-concepts.html There ...
- Nuxt / Vue.js in TypeScript: Object literal may only specify known properties, but 'components' does not exist in type 'VueClass'
项目背景, Nuxt(vue), TypeScript 生成完项目框架, 添加测试demo页面. 在生成的模板代码中添加layout配置如下: <script lang="ts&quo ...
- codevs 2800 送外卖 TSP问题
2800 送外卖 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份 ...
- JMeter常用的调试工具
JMeter常用的调试工具有如下五种: 1.View Tree:查看结果树.含请求信息.响应信息等,请求头信息中的cookie信息一般默认不会显示,可通过修改JMeter配置参数进行显示.日常大家用的 ...
- cell 配置
Cells Cell configuration options Configure the API (top-level) cell Configure the child cells Config ...
- WPF中如何在文本外面加虚线外框
WPF中如何在文本外面加虚线外框 昨天突然被问到如何在wpf里面给一段文本加个虚线外框,由于有一段时间没玩wpf了,一时还真没想出来,虽然大概有个思路,但是也不保证正确.今天回到家,闲着没事情也就随便 ...
- ASP.NET Core 开源项目整理
前言: 对 .NET Core 的热情一直没有下降过,新起的项目几乎都是采用 Core 来做开发. 跨平台是一个方面,另外就是 Core 很轻,性能远超很多开发语言(不坑). 一.ASP.NET Co ...
- Container容器调用构造函数