Python的itertools模块

Python的itertools模块

itertools 模块用来产生不同类型迭代器的函数或类,这些函数的返回都是一个迭代器。
可以通过for循环来遍历取值,也可以使用next()取值

模块提供的迭代器函数类型

  • 无限迭代器:生成一个无限序列
  • 有限迭代器:接收一个或多个序列作为参数,进行组合、分组和过滤
  • 组合生成器:序列的排列、组合

无限迭代器

提供了三个函数(实际上是累)来生成无限序列迭代器

  • count(firstval=0, step=1)
    创建一个从 firstval (默认值为 0) 开始,以 step (默认值为 1) 为步长的的无限整数迭代器

  • cycle(iterable)
    对 iterable 中的元素反复执行循环,返回迭代器

  • repeat(object [,times]
    反复生成 object,如果给定 times,则重复次数为 times,否则为无限

count

count() 接收两个参数,第一个参数指定开始值,默认为 0,第二个参数指定步长,默认为 1

import itertools
nums = itertools.count(10,2)	# 开始值和步长度
for i in nums:
	if i > 20:
		break
	print(i)

cycle

cycle() 用于对 iterable 中的元素反复执行循环

import itertools
strings = itertools.cycle('ABC')
i = 1
for s in strings:
	if i == 10:
		break
	print(s)
	i += 1

repeat

repeat() 用于反复生成一个 object

import itertools
for item in itertools.repeat('hello world', 3):
	print(item)

有限迭代器操作

itertools 提供的几个迭代器操作函数更加有用

chain

chain() 可以把一组迭代对象串联起来,形成更大的迭代器

import itertools
a = [[1,2],[3,4],[5,6]]
for c in itertools.chain(*a):
	print(c, end=' ')
# 1 2 3 4 5 6

chain.from_iterable

接收一个可迭代对象作为参数,返回一个迭代器

from itertools import chain

string = chain.from_iterable('ABCD')
print(next(string))
# A

groupby

groupby(iterable[, keyfunc])
iterable 是一个可迭代对象,keyfunc 是分组函数,用于对 iterable 的连续项进行分组。
如果不指定,则默认对 iterable 中的连续相同项进行分组,返回一个 (key, sub-iterator) 的迭代器。

import itertools
for k, v in itertools.groupby('AaaBBbcCAAa', lambda x : x.upper()):
	print(k, list(v))
# A ['A', 'a', 'a']
# B ['B', 'B', 'b']
# C ['c', 'C']
# A ['A', 'A', 'a']

compress

compress(data, selectors)
可用于对数据进行筛选,当 selectors 的某个元素为 true 时,则保留 data 对应位置的元素,否则去除:

from itertools import compress

result = compress('ABCDEF', [1,True, 0, False])
print(list(result))
# ['A', 'B']

dropwhile

dropwhile(predicate, iterable)
predicate 是函数,iterable 是可迭代对象。
对于 iterable 中的元素,如果 predicate(item) 为 true,则丢弃该元素,否则返回该项及所有后续项。

from itertools import dropwhile

result = dropwhile(lambda x: x < 5, [1,2,4,5,6,8,5])
print(list(result))
# [5, 6, 8, 5]

islice

islice(iterable, [start,] stop [, step])
islice 是切片选择,它的使用形式如下
iterable 是可迭代对象,start 是开始索引,stop 是结束索引,step 是步长,start 和 step 可选。

from itertools import islice

print(list(islice([1,2,3,4], 3)))
# [1, 2, 3]

tee

tee(iterable [,n])
tee 用于从 iterable 创建 n 个独立的迭代器,以元组的形式返回,n 的默认值是 2

from itertools import tee
iter1, iter2 = tee('abcde')
print(list(iter1))
print(list(iter2))

# ['a', 'b', 'c', 'd', 'e']
# ['a', 'b', 'c', 'd', 'e']

组合生成器

producr

product(iter1, iter2, … iterN, [repeat=1])
product 用于求多个可迭代对象的笛卡尔积,它跟嵌套的 for 循环等价。

from itertools import product
for i in product('abcd','12'):
	print(i)

permutations

permutations(iterable[, r])
permutations 用于生成一个排列
r 指定生成排列的元素的长度,如果不指定,则默认为可迭代对象的元素长度。

from itertools import permutations
print(list(permutations('abc',2)))
# [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]

combinations

combinations(iterable, r)
combinations 用于求序列的组合

from itertools import combinations
print(list(combinations('abc',2)))
# [('a', 'b'), ('a', 'c'), ('b', 'c')]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值