该库为满足特定需要的比较高效的迭代器内置库,在数据科学中的应用也不少,故有必要了解一下:
import itertools
import sys
无限迭代器(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 |
a=itertools.count(5)
type(a)
itertools.count
next(a)
5
next(a)
6
next(a)
7
b=itertools.count(6,2)
next(b)
6
next(b)
8
a=itertools.cycle([1,2])
next(a)
1
next(a)
2
next(a)
1
next(a)
2
b=itertools.cycle('ABC')
next(b)
'A'
next(b)
'B'
next(b)
'C'
next(b)
'A'
a=itertools.repeat([1,2,3])
next(a)
[1, 2, 3]
next(a)
[1, 2, 3]
b=itertools.repeat('ABC',3)
next(b)
'ABC'
next(b)
'ABC'
next(b)
'ABC'
try:
next(b)
except Exception as e:
print(sys.exc_info())
(<class 'StopIteration'>, StopIteration(), <traceback object at 0x0000024CB3EE2E08>)
最短输入停止迭代器(Iterators terminating on the shortest input sequence)
Iterator |
Arguments |
Results |
Example |
accumulate() |
p [,func] |
p0, p0+p1, p0+p1+p2, … |
accumulate([1,2,3,4,5]) --> 1 3 6 10 15 |
chain() |
p, q, … |
p0, p1, … plast, q0, q1, … |
chain('ABC', 'DEF') --> A B C D E F |
chain.from_iterable() |
iterable |
p0, p1, … plast, q0, q1, … |
chain.from_iterable(['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 |
filterfalse() |
pred, seq |
elements of seq where pred(elem) is false |
filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 |
groupby() |
iterable[, key] |
sub-iterators grouped by value of key(v) |
|
islice() |
seq, [start,] stop [, step] |
elements from seq[start:stop:step] |
islice('ABCDEFG', 2, None) --> C D E F G |
starmap() |
func, seq |
func(seq[0]), func(seq[1]), … |
starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 |
takewhile() |
pred, seq |
seq[0], seq[1], until pred fails |
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 |
tee() |
it, n |
it1, it2, … itn splits one iterator into n |
|
zip_longest() |
p, q, … |
(p[0], q[0]), (p[1], q[1]), … |
zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
a=itertools.accumulate([1,2,3,4])
list(a)
[1, 3, 6, 10]
b=itertools.accumulate('abc')
list(b)
['a', 'ab', 'abc']
a=itertools.chain([1,2],[3,4],[5,6])
list(a)
[1, 2, 3, 4, 5, 6]
b=itertools.chain('ab','cd')
list(b)
['a', 'b', 'c', 'd']
- itertools.chain.from_iterable
a=itertools.chain.from_iterable(['abc','def'])
list(a)
['a', 'b', 'c', 'd', 'e', 'f']
a=itertools.compress('abcdef',[1,0,1,0,1,1])
list(a)
['a', 'c', 'e', 'f']
a=itertools.compress('abcdef',[1,False,1,False,1,0])
list(a)
['a', 'c', 'e']
a=itertools.dropwhile(lambda x:x<5,[1,4,6,4,1])
list(a)
[6, 4, 1]
a=itertools.takewhile(lambda x:x<5,[1,4,6,4,1])
list(a)
[1, 4]
a=itertools.filterfalse(lambda x:x%2,range(10))
list(a)
[0, 2, 4, 6, 8]
x = [(1, 2), (2, 3), (1, 4), (5, 5), (3, 4), (2, 6)]
a=itertools.groupby(x,lambda x:x[0])
for i in a:
print(i[0],list(i[1]))
1 [(1, 2)]
2 [(2, 3)]
1 [(1, 4)]
5 [(5, 5)]
3 [(3, 4)]
2 [(2, 6)]
a=itertools.islice('abcdefg',2,None)
list(a)
['c', 'd', 'e', 'f', 'g']
b=itertools.islice('abcdefg',2,4)
list(b)
['c', 'd']
a=itertools.starmap(pow,[(2,5),(3,2)])
list(a)
[32, 9]
a=itertools.tee([1,2,3],2)
list(a)
[<itertools._tee at 0x24cb3f29448>, <itertools._tee at 0x24cb3f29888>]
a=itertools.tee([1,2,3],2)
list(a[0])
[1, 2, 3]
list(a[1])
[1, 2, 3]
a=itertools.zip_longest('abcd','xy',fillvalue='-')
list(a)
[('a', 'x'), ('b', 'y'), ('c', '-'), ('d', '-')]
组合迭代器
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 |
a=itertools.product([1,2,3],[4,5,6])
list(a)
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
a=itertools.product([1,2,3],[4,5,6],repeat=2)
list(a)
[(1, 4, 1, 4),
(1, 4, 1, 5),
(1, 4, 1, 6),
(1, 4, 2, 4),
(1, 4, 2, 5),
(1, 4, 2, 6),
(1, 4, 3, 4),
(1, 4, 3, 5),
(1, 4, 3, 6),
(1, 5, 1, 4),
(1, 5, 1, 5),
(1, 5, 1, 6),
(1, 5, 2, 4),
(1, 5, 2, 5),
(1, 5, 2, 6),
(1, 5, 3, 4),
(1, 5, 3, 5),
(1, 5, 3, 6),
(1, 6, 1, 4),
(1, 6, 1, 5),
(1, 6, 1, 6),
(1, 6, 2, 4),
(1, 6, 2, 5),
(1, 6, 2, 6),
(1, 6, 3, 4),
(1, 6, 3, 5),
(1, 6, 3, 6),
(2, 4, 1, 4),
(2, 4, 1, 5),
(2, 4, 1, 6),
(2, 4, 2, 4),
(2, 4, 2, 5),
(2, 4, 2, 6),
(2, 4, 3, 4),
(2, 4, 3, 5),
(2, 4, 3, 6),
(2, 5, 1, 4),
(2, 5, 1, 5),
(2, 5, 1, 6),
(2, 5, 2, 4),
(2, 5, 2, 5),
(2, 5, 2, 6),
(2, 5, 3, 4),
(2, 5, 3, 5),
(2, 5, 3, 6),
(2, 6, 1, 4),
(2, 6, 1, 5),
(2, 6, 1, 6),
(2, 6, 2, 4),
(2, 6, 2, 5),
(2, 6, 2, 6),
(2, 6, 3, 4),
(2, 6, 3, 5),
(2, 6, 3, 6),
(3, 4, 1, 4),
(3, 4, 1, 5),
(3, 4, 1, 6),
(3, 4, 2, 4),
(3, 4, 2, 5),
(3, 4, 2, 6),
(3, 4, 3, 4),
(3, 4, 3, 5),
(3, 4, 3, 6),
(3, 5, 1, 4),
(3, 5, 1, 5),
(3, 5, 1, 6),
(3, 5, 2, 4),
(3, 5, 2, 5),
(3, 5, 2, 6),
(3, 5, 3, 4),
(3, 5, 3, 5),
(3, 5, 3, 6),
(3, 6, 1, 4),
(3, 6, 1, 5),
(3, 6, 1, 6),
(3, 6, 2, 4),
(3, 6, 2, 5),
(3, 6, 2, 6),
(3, 6, 3, 4),
(3, 6, 3, 5),
(3, 6, 3, 6)]
for i in itertools.permutations([1,2,3],2):
print (i)
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
for i in itertools.permutations('123',2):
print (i)
('1', '2')
('1', '3')
('2', '1')
('2', '3')
('3', '1')
('3', '2')
for i in itertools.permutations('122',2):
print (i)
('1', '2')
('1', '2')
('2', '1')
('2', '2')
('2', '1')
('2', '2')
a=itertools.combinations([1,2,3],2)
list(a)
[(1, 2), (1, 3), (2, 3)]
a=itertools.combinations('abcd',2)
list(a)
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
- itertools.combinations_with_replacement
a=itertools.combinations_with_replacement('abcd',2)
list(a)
[('a', 'a'),
('a', 'b'),
('a', 'c'),
('a', 'd'),
('b', 'b'),
('b', 'c'),
('b', 'd'),
('c', 'c'),
('c', 'd'),
('d', 'd')]
#####
愿你一寸一寸地攻城略地,一点一点地焕然一新
#####
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix