itertools: 迭代器小工具


  Function

Infinite iterators

(无限迭代)

count() cycle() repeat()  

Combinatoric generators

(排列组合)

product() permutations() combinations()

combinations_with_replacement()

有限迭代 accumulate(p[, func]) chain(p, q, ...) chain.from_iterable([p, q, ...]) compress(data, selectors)
dropwhile(pred, seq) groupby(iterable[, keyfunc]) filterfalse(pred, seq) islice(seq, [start,] stop [, step])
starmap(fun, seq) tee(it, n=2) takewhile(pred, seq) zip_longest(p, q, ...)


itertools.tee(it, n=2)
复制独立的迭代器(默认复制两次)

1 import itertools
2 num = [1, 2, 3, 4]
3 for i, item in enumerate(itertools.tee(num, 3)):   # 3是指复制三次,默认为2次
4     print('第{}个迭代对象'.format(i))
5     for it in item:
6         print(it, end=' ')
7     print('')

 

 itertools.accumulate(iter[, func, initial])   对iter中对象进行指定函数的迭代运算

1 for item in itertools.accumulate(num):    # 默认迭代相加
2     print(item, end='  ')
3 print('')
4 for item in itertools.accumulate(num, func=operator.mul):   # 指定迭代函数
5     print(item, end='  ')
6 print('')
7 for item in itertools.accumulate(num, func=operator.mul, initial=2):  # 指定迭代初始值
8     print(item, end='  ')
9 print('')

 

itertools.chain ( *iterables ) # 一次性遍历多个迭代器

1 # itertools. chain ( *iterables )
2 for item in itertools.chain(itertools.tee(num, 3)):
3     for i in item:
4         print(i, end='  ')
5 print(''

itertools.chain.from_iterable ( *iterables ) # 一次性遍历多个迭代器

1 # itertools. chain.from_iterable ( *iterables )
2 for item in itertools.chain.from_iterable(itertools.tee(num, 3)):
3     print(item, end='  ')
4 print('')

 

itertools. combinations ( iterable, r )     # 组合,从iterable对象中抽取r个对象的所有情况

1 # itertools. combinations ( iterable, r )
2 for item in itertools.combinations (num, 3):
3     print(item, end='  ')
4 print('')

 

itertools.combinations_with_replacement( iterable, r )     # 组合,从iterable对象中抽取r个对象的所有情况

抽取r次,每次随机抽取,会出现重复对象,适合独立重复试验

1 # itertools. combinations_with_replacement ( iterable, r )
2 for item in itertools.combinations_with_replacement(num, 2):
3     print(item, end='  ')
4 print('')

 

itertools.compress(data, selectors)   返回selectors中计算结果为True的相应位置中的data数据

1 # itertools.compress ( data, selectors )
2 for item in itertools.compress('ABCDEF', [1, 0, 1, 0, 1, 1]):
3     print(item, end=' ')
4 print()

 

itertools.count( start = 0, step = 1 )   从0开始,生成步长为1的无限序列

1 # itertools. count ( start=0, step=1 )
2 for item in itertools.count(start=3, step=3):
3     print(item, end='  ')
4     if item > 20:
5         break
6 print('')

 

itertools.cycle(iterable)   对iterable对象中的元素进行无限循环

1 # itertools. cycle ( iterable )
2 for item in itertools.cycle(num):
3     print(item, end=' ')
4 print()

 

 itertools.dropwhile(predicate, iterable)  遍历iterable,删除满足predicate条件的元素,直到出现不满足条件的元素,输出剩余元素

1 # itertools. dropwhile ( predicate, iterable )
2 for item in itertools.dropwhile(lambda x: x < 5, [1, 4, 6, 3, 0]):
3     print(item, end=' ')
4 print()

 

itertools.filterfalse ( predicate, iterable )  遍历iterable,返回满足predicate条件的为False的所有元素

1 # itertools.filterfalse ( predicate, iterable )
2 for item in itertools.filterfalse(lambda x: x % 2, range(10)):
3     print(item, end=' ')
4 print()

 

itertools. groupby ( iterable, key=None ) 对iterable做分组处理,但是该处分组不同于sql中,连续输入同一个字符算作一组,重新打断输入则为另外一组

1 # itertools. groupby ( iterable, key=None )
2 for k, g in itertools.groupby('AAAABBBCCDAABBB'):
3     print(k, list(g))
4 for k, g in itertools.groupby('AAAABBBCCDAABBB', key=lambda x: x == 'A'):
5     print(k, list(g))

                                 

 

itertools.islice( iterable, start, stop [, step ] )  对literable进行切片处理

itertools.islice ( iterable, stop )

 1 # itertools.islice ( iterable, stop )
 2 # itertools.islice ( iterable, start, stop [, step ] )
 3 for item in itertools.islice('ABCDEFG', 2):
 4     print(item, end=' ')
 5 print('')
 6 for item in itertools.islice('ABCDEFG', 2, 4):
 7     print(item, end=' ')
 8 print('')
 9 for item in itertools.islice('ABCDEFG', 2, None, 2):
10     print(item, end=' ')
11 print('')

itertools.permutations(iterable, r=None)   排列,抽取r个iterable对象进行排列,当r=None时,对所有元素进行排列

1 # itertools. permutations ( iterable, r=None )
2 for item in itertools.permutations('ABC', 2):
3     print(item, end=' ')
4 print('')

 

itertools.product(*iterable, repeat=1)    计算笛卡尔积,repeat为iterable所有对象的重复次数

1 # itertools. product ( *iterables, repeat=1 )
2 for item in itertools.product('AB', 'xy', repeat=2):
3     print(item)
4 print('')

 

 

# itertools. repeat ( object [, times ] )
repeat(10, 3) --> 10 10 10

# itertools. starmap ( function, iterable )
starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

# itertools. takewhile ( predicate, iterable ) 遇到不满足条件的即停止运行
takewhile(lambda x: x<5, [1,4,6,3,5]) --> 1 4

# itertools. zip_longest ( *iterables, fillvalue=None )
zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-

 

 

 

 

 

 

 

 

posted @ 2021-12-30 13:58  程序猿Time  阅读(175)  评论(0编辑  收藏  举报