码迷,mamicode.com
首页 > 编程语言 > 详细

python - itertools 模块

时间:2019-06-14 21:47:49      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:链接   lambda   split   c函数   red   tar   filter   click   选择   

相关文档

文档 链接 

pymotw 链接 

无限迭代器

itertools.count()

说明

生成一个无限迭代的数字队列, 只有进行 

参数 

其实数字以及步幅

start, [step]

返回值 

start, start+step, start+2*step, ...

示例

count(10) --> 10 11 12 13 14 ...
技术图片
from itertools import *
import time


c = count(10,5)
print c


for i in c:
    time.sleep(0.5)
    print i

"""
count(10, 5)
10
15
20
25
30
35
40
45
50
55

Process finished with exit code -1
"""
详细示例

itertools.cycle()

说明

传入一个可迭代对象然后循环遍历此对象

参数 

p

返回值 

p0, p1, ... plast, p0, p1, ...

示例

cycle(‘ABCD‘) --> A B C D A B C D ...
技术图片
from itertools import *
import time

c = cycle([1, 2, 3])
print c

for i in c:
    time.sleep(0.5)
    print i

"""
<itertools.cycle object at 0x0000000002A42088>
1
2
3
1
2
3
1
2

Process finished with exit code -1

"""
详细示例

itertools.repeat()

说明

传入一个对象, 以及数字, 遍历此对象指定数字次数

参数 

elem [,n]

返回值 

elem, elem, elem, ... endlessly or up to n times

示例

repeat(10, 3) --> 10 10 10
技术图片
from itertools import *
import time

c = repeat(3, 5)
print c

for i in c:
    time.sleep(0.5)
    print i

"""
repeat(3, 5)
3
3
3
3
3

Process finished with exit code 0
"""
详细示例

处理输入序列迭代器

itertools.chain()

说明

传入多个序列, 遍历时将多个序列串联一起进行

参数 

p, q, ...

返回值 

p0, p1, ... plast, q0, q1, ...

示例

chain(‘ABC‘, ‘DEF‘) --> A B C D E F
技术图片
from itertools import *

c = chain(range(5), range(3))
print c

for i in c:
    print i

"""
<itertools.chain object at 0x0000000002A77FD0>
0
1
2
3
4
0
1
2

Process finished with exit code 0r
"""
详细示例

itertools.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
技术图片
from itertools import *

c = compress("hahahahahaha", [1, 0, 1, 0, 1, 0, 1, 0])
print c

for i in c:
    print i

"""
<itertools.compress object at 0x0000000002B01048>
h
h
h
h

Process finished with exit code 0
"""
详细示例

itertools.dropwhile()

说明

创建一个迭代器,只要函数predicate(item)为True,就丢弃iterable中的项,如果predicate返回False,就会生成iterable中的项和所有后续项。

即:在条件为false之后的第一次, 返回迭代器中剩下来的项.

参数 

pred, seq

返回值 

 seq[n], seq[n+1], starting when pred fails

示例

 dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
def dropwhile(predicate, iterable):
    # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    iterable = iter(iterable)
    for x in iterable:
        if not predicate(x):
            yield x
            break
    for x in iterable:
        yield x
技术图片
from itertools import *
  
 
def should_drop(x):
    print Testing:, x
    return (x<1)
 
for i in dropwhile(should_drop, [ -1, 0, 1, 2, 3, 4, 1, -2 ]):
    print Yielding:, i
 
Testing: -1
Testing: 0
Testing: 1
Yielding: 1
Yielding: 2
Yielding: 3
Yielding: 4
Yielding: 1
Yielding: -2
详细示例

itertools.groupby()

说明

按照keyfunc函数对序列每个元素执行后的结果分组(每个分组是一个迭代器), 返回这些分组的迭代器

参数 

iterable[, keyfunc]

返回值 

sub-iterators grouped by value of keyfunc(v)

示例

chain(‘ABC‘, ‘DEF‘) --> A B C D E F

 

迭代器         参数            结果                                        例子
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-

 

python - itertools 模块

标签:链接   lambda   split   c函数   red   tar   filter   click   选择   

原文地址:https://www.cnblogs.com/shijieli/p/11025224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!