“专业人士笔记”系列目录:
创帆云:Python成为专业人士笔记--强烈建议收藏!每日持续更新!
Combinations方法
组合将返回一个列表的k-wise散列组合序列的生成器
换句话说:它将返回一个元组生成器,其中包含输入列表的所有可能的 k-wise散列 组合
例如:
如果你有一个列表:
import itertools
a = [1,2,3,4,5]
b = list(itertools.combinations(a, 2))
print(b)
# [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
上面的输出是一个生成器,转换为输入列表a的所有可能成对组合的元组列表
你也可以找到所有的3值组合:
import itertools
a = [1,2,3,4,5]
b = list(itertools.combinations(a, 3))
print(b)
# [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]
dropwhile方法
dropwhile允许当所调用功能在首次返回False后从序列中提取剩余项:
import itertools
def is_even(x):
return x % 2 == 0 # 取模等于0 则返回True,否则返回False
lst = [0, 2, 4, 12, 18, 13, 14, 22, 23, 44]
result = list(itertools.dropwhile(is_even, lst))
print(result)
# [13, 14, 22, 23, 44]
# 注意:在元素13取模不等于0返回False后,后面的元素无论调用功能返回的结果是什么无所谓了,都会将剩余的元素返回到列表中。
zip_longest 方法
其函数实现的功能和内置zip函数大致相同(实现一一对应),不过内置的zip函数是已元素最少对象为基准,而zip_longest函数是以元素最多对象为基准,我们来看例子:
from itertools import zip_longest
a = [i for i in range(5)] # 长度为 5
b = ['a', 'b', 'c', 'd', 'e'] # 长度为 5
for i in zip_longest(a, b):
x, y = i
print(x, y)
# 输出 :4 e 这里效果和zip函数一样
再来一段代码,两个变量长度不一致时:
a = [i for i in range(5)] # 长度为 5
b = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] # 长度为 7
for i in zip_longest(a, b):
x, y = i
print(x, y)
#输出:None g 注意,这里以长度最长的值为准返回
迭代器切片
islice 允许你对一个迭代器切片 :
import itertools
def gen():
n = 0
while n < 20:
n += 1
yield n
# 正常情况下,gen函数应该要循环20次,每次返回n+1的值
for part in itertools.islice(gen(), 4):
print(part)
#输出:
1
2
3
4
利用islice函数的第2个参数,迭代器在运行到第4次的时候中断了
注意,你在islice函数中也可以同时使用start、stop和step参数,像这样调用:
itertools.islice(iterable, 1, 30, 3) #从第一个开始,运行第30个结束,每隔3个运行一次
自己在python3云环境中试试效果吧
takewhile
itertools.takewhile和dropwhile用法很像,区别是调用功能在首次返回False后从之前序列中提取项 :用上面dropwhile的例子小改一下:
import itertools
def is_even(x):
return x % 2 == 0 # 取模等于0 则返回True,否则返回False
lst = [0, 2, 4, 12, 18, 13, 14, 22, 23, 44]
result = list(itertools.takewhile(is_even, lst))
print(result)
# [0, 2, 4, 12, 18]
# 注意:在元素13取模不等于0返回False后,后面的元素无论调用功能返回的结果是什么无所谓了,都会将之前的元素返回到列表中。
permutations
itertools.permutations 返回具有可迭代元素的组合排列的生成器:
注意其与combinations函数的本质区别,仔细体会下面的代码:
import itertools
a = [1,2,3]
print(list(itertools.permutations(a)))
# [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
#不传递第2个参数,则默认是按3个元素组合
print(list(itertools.permutations(a, 2)))
# [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
#传递了第2个参数,按两两所有可能性组合
如果列表a有重复的元素,那么产生的排列也将有重复的元素,你可以使用set来获得唯一的排列 :
a = [1,2,1]
list(itertools.permutations(a))
# [(1, 2, 1), (1, 1, 2), (2, 1, 1), (2, 1, 1), (1, 1, 2), (1, 2, 1)]
set(itertools.permutations(a))
# {(1, 1, 2), (1, 2, 1), (2, 1, 1)}
# 利用set集合不能存储重复值的特性过滤重复值
repeat
重复n次,直接看代码:
import itertools
for i in itertools.repeat('over-and-over', 3):
print(i)
# over-and-over
over-and-over
over-and-over
求累计值
import itertools as it
import operator
print(list(it.accumulate([1,2,3,4,5])))
# [1, 3, 6, 10, 15] 默认是顺序累计加和
print(list(it.accumulate([1,2,3,4,5], func=operator.mul))
)
# [1, 2, 6, 24, 120] 使用相乘算法
循环重复迭代
import itertools as it
cycle_iterator = it.cycle('abc123')
print([next(cycle_iterator) for i in range(0, 100)])
# ['a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1']
product
这个函数允许你迭代表的笛卡尔积 :
from itertools import product
a=[1,2,3,4]
b=['a','b','c']
for i in product(a,b):
print(i)
# (1, 'a')
(1, 'b')
(1, 'c')
(2, 'a')
(2, 'b')
(2, 'c')
(3, 'a')
(3, 'b')
(3, 'c')
(4, 'a')
(4, 'b')
(4, 'c')
count
这个简单的函数产生无穷级数的数字,例如:
import itertools
for number in itertools.count():
if number > 20:
break
print(number)
# 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
注意:该函数必须搭配break使用,否则将陷入无穷循环!
参数:
count() 可以传入两个参数: start 和 step:
import itertools
for number in itertools.count(start=10, step=4):
print(number)
if number > 20:
break
# 10
14
18
22
链接多个迭代器
itertools.chain可以创建单个生成式,该生成式将按顺序产生来自多个生成式迭代器的值 :
from itertools import chain
a = (x for x in ['1', '2', '3', '4'])
b = (x for x in ['x', 'y', 'z'])
print(' '.join(chain(a, b)))
# 1 2 3 4 x y z
以上代码均已通过python3云环境调试,任何错误可随时与我联系。请勿转载,谢谢