Python标准库:迭代器Itertools
Infinite Iterators:
| Iterator | Arguments | Results | Example |
|---|---|---|---|
count() |
start, [step] | start, start+step, start+2*step, ... | count(10) --> 10 11 12 13 14 ... |
cycle() |
p | p0, p1, ... plast, p0, p1, ... | cycle('ABCD') --> A B C D A B C D ... |
repeat() |
elem [,n] | elem, elem, elem, ... endlessly or up to n times | repeat(10, 3) --> 10 10 10 |
Iterators terminating on the shortest input sequence:
| Iterator | Arguments | Results | Example |
|---|---|---|---|
chain() |
p, q, ... | p0, p1, ... plast, q0, q1, ... | chain('ABC', 'DEF') --> A B C D E F |
compress() |
data, selectors | (d[0] if s[0]), (d[1] if s[1]), ... | compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F |
dropwhile() |
pred, seq | seq[n], seq[n+1], starting when pred fails | dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 |
groupby() |
iterable[, keyfunc] | sub-iterators grouped by value of keyfunc(v) | |
ifilter() |
pred, seq | elements of seq where pred(elem) is true | ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9 |
ifilterfalse() |
pred, seq | elements of seq where pred(elem) is false | ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 |
islice() |
seq, [start,] stop [, step] | elements from seq[start:stop:step] | islice('ABCDEFG', 2, None) --> C D E F G |
imap() |
func, p, q, ... | func(p0, q0), func(p1, q1), ... | imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000 |
starmap() |
func, seq | func(*seq[0]), func(*seq[1]), ... | starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 |
tee() |
it, n | it1, it2, ... itn splits one iterator into n | |
takewhile() |
pred, seq | seq[0], seq[1], until pred fails | takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 |
izip() |
p, q, ... | (p[0], q[0]), (p[1], q[1]), ... | izip('ABCD', 'xy') --> Ax By |
izip_longest() |
p, q, ... | (p[0], q[0]), (p[1], q[1]), ... | izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
Combinatoric generators:
| Iterator | Arguments | Results |
|---|---|---|
product() |
p, q, ... [repeat=1] | cartesian product, equivalent to a nested for-loop |
permutations() |
p[, r] | r-length tuples, all possible orderings, no repeated elements |
combinations() |
p, r | r-length tuples, in sorted order, no repeated elements |
combinations_with_replacement() |
p, r | r-length tuples, in sorted order, with repeated elements |
product('ABCD', repeat=2) |
AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD |
|
permutations('ABCD', 2) |
AB AC AD BA BC BD CA CB CD DA DB DC |
|
combinations('ABCD', 2) |
AB AC AD BC BD CD |
|
combinations_with_replacement('ABCD', 2) |
AA AB AC AD BB BC BD CC CD DD |
itertools.chain(*iterables)itertools.combinations(iterable, r)itertools.combinations_with_replacement(iterable, r)(n+r-1)! / r! / (n-1)!
itertools.compress(data, selectors)def compress(data, selectors):
# compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
return (d for d, s in izip(data, selectors) if s)
itertools.count(start=0, step=1)itertools.cycle(iterable)itertools.dropwhile(predicate, iterable)itertools.groupby(iterable[, key])- groupby([0, 0, 0, 1, 1, 1, 2, 2, 2]) --> (0, (0 0 0)) (1, (1 1 1)) (2, (2 2 2))
itertools.ifilter(predicate, iterable)itertools.ifilterfalse(predicate, iterable)itertools.imap(function, *iterables)itertools.izip(*iterables)itertools.islice(iterable, start, stop[, step])这种方法假设后面仅仅加两个參数,那么第二个參数是slice的结束。
itertools.izip_longest(*iterables[, fillvalue])
itertools.permutations(iterable[, r])itertools.product(*iterables[, repeat])
itertools.repeat(object[, times])itertools.takewhile(predicate, iterable)itertools.tee(iterable[, n=2])itertools.starmap(function, iterable)Python标准库:迭代器Itertools的更多相关文章
- Python标准库笔记(10) — itertools模块
itertools 用于更高效地创建迭代器的函数工具. itertools 提供的功能受Clojure,Haskell,APL和SML等函数式编程语言的类似功能的启发.它们的目的是快速有效地使用内存, ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- 转--Python标准库之一句话概括
作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...
- Python标准库笔记(11) — Operator模块
Operator--标准功能性操作符接口. 代码中使用迭代器时,有时必须要为一个简单表达式创建函数.有些情况这些函数可以用一个lambda函数实现,但是对于某些操作,根本没必要去写一个新的函数.因此o ...
- python 标准库大全
python 标准库 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata:Unicode字符数据库 string ...
- [python标准库]XML模块
1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
- Python 标准库一览(Python进阶学习)
转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...
- Python 标准库之 xml.etree.ElementTree
Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- Python标准库 re
正则表达式 regular expression 用来匹配一系列符合句法规则的字符串,是一门独立的小型的语言,如果你了解类Unix系统,那么你对正则表达式就一定不陌生.正则表达式的概念最初是由Unix ...
随机推荐
- logback.xml_appender配置
logback<appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的组件. < ...
- android总结
针对Android有以下几点需要注意: 1.是不是应该把数据刷新操作放在onResume()中? @Override public void onResume() { ...
- C# 邮件发送系统
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- String.Format格式说明
原文地址:http://www.cnblogs.com/tuyile006/archive/2006/07/13/449884.aspx C#格式化数值结果表 字符 说明 示例 输出 C 货币 str ...
- 警惕javascript代码中的“</script>”!
之前在写<博客园自定义博客侧边栏公告的过滤漏洞>的时候遇到了一个javascript代码报错“语法错误”的问题,一直不得以解决,感谢Arliang发现了并为我进行了耐心的解释,现整理如下: ...
- [LeetCode] Add Digits (a New question added)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Python闭包与javascript闭包比较
实例一 python def line_conf(): def line(x): return 2*x+1 print(line(5)) # within the scope line_con ...
- (转载)OC学习篇之---第一个程序HelloWorld
之前的一片文章简单的介绍了OC的相关概述,从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这 ...
- Java-note-调试小技巧
使用 Eclipse 调试 Java 程序的 10 个技巧 英文原文:Again! – 10 Tips on Java Debugging with Eclipse 你应该看过一些如<关于调试的 ...
- [Hive - LanguageManual] Create/Drop/Alter -View、 Index 、 Function
Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version info ...