📜  Python中的迭代器函数2(islice(),starmap(),tee()..)

📅  最后修改于: 2020-07-07 05:02:46             🧑  作者: Mango

Python中的迭代器函数 1
1. islice(迭代器,开始,停止,步长):此迭代器有选择地打印在其作为参数传递的可迭代容器中提到的值。该迭代器有4个参数,可迭代的容器,开始位置,结束位置和步长。
2. starmap(func, tuple list):此迭代器将一个函数和一个tuple列表作为参数,并从列表的每个tuple中根据该函数返回

# Python代码,展示islice()和starmap()
# 导入"itertools"
import itertools
# 初始化list
li = [2, 4, 5, 7, 8, 10, 20]
# 初始化tuple list
li1 = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10 , 1) ]
# 使用islice()
print ("切片后的list : ",end="")
print (list(itertools.islice(li,1, 6, 2)))
# 使用starmap()
print ("应用函数之后的值 : ",end="")
print (list(itertools.starmap(min,li1)))

输出:

切片后的list : [4, 7, 10]
应用函数之后的值 : [1, 1, 4, 1]

3. takewhile(func,iterable):此迭代器与dropwhile()相反,它打印值,直到函数第一次返回false为止
4. tee(iterator,count):此迭代器将容器拆分为参数中提到的多个迭代器

# Python代码展示如何使用takewhile()和tee()
# 导入"itertools"
import itertools
# 初始化list
li = [2, 4, 6, 7, 8, 10, 20]
# 把list存入迭代器
iti = iter(li)
# 会用takewhile().
print ("遇到第一个false的值 : ",end="")
print (list(itertools.takewhile(lambda x : x%2==0,li )))
# 使用tee()
it = itertools.tee(iti, 3)
# 打印结果
print ("迭代器 : ")
for i in range (0,3):
    print (list(it[i]))

输出:

遇到第一个false的值 : [2, 4, 6]
迭代器 :
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]

5. zip_longest(iterable1,iterable2,fillval):此迭代器按顺序交替输出iterable。如果可迭代项目之一已完全打印,则剩余值将分配给fillvalue的值填充

# Python代码,展示如何使用zip_longest()
# 导入"itertools"
import itertools
# 使用zip_longest()联合两个迭代器.
print ("联合后的值 : ")
print (*(itertools.zip_longest('GesoGes','ekfrek',fillvalue='_' )))

输出:

联合后的值  :
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')

组合迭代器

1. product(iter1,iter2):此迭代器打印作为参数传递的两个可迭代容器的笛卡尔积
2. permutations(iter,group_size):此迭代器可打印iterable所有元素的所有可能排列。每个置换组的大小由group_size参数决定

# Python代码,展示product()和permutations()
# 导入"itertools"
import itertools
# 使用product()
print ("笛卡尔积 : ")
print (list(itertools.product('AB','12')))
# 使用permutations
print ("所有可能的组合 : ")
print (list(itertools.permutations('GfG',2)))

输出:

笛卡尔积 :
[('A', '1'), ('A', '2'), ('B', '1'), ('B', '2')]
所有可能的组合 :
[('G', 'f'), ('G', 'G'), ('f', 'G'), ('f', 'G'), ('G', 'G'), ('G', 'f')]

3. groups(iterable,group_size):此迭代器按排列顺序打印在指定组大小中传入参数的容器的所有可能组合(不替换)
4. combinations_with_replacement(iterable,GROUP_SIZE) :这个方法,打印所有可能的组合(与替换)。

# Python代码,展示如何使用combination()和combination_with_replacement()
# 导入"itertools"
import itertools
# 使用combinations()
print ("所有组合,排序后的结果是 : ")
print (list(itertools.combinations('1234',2)))
# 使用combinations_with_replacement()
print ("所有组合,排序后的结果是 : ")
print (list(itertools.combinations_with_replacement('GfG',2)))

输出:

所有组合,排序后的结果是 :
[('1', '2'), ('1', '3'), ('1', '4'), ('2', '3'), ('2', '4'), ('3', '4')]
所有组合,排序后的结果是 :
[('G', 'G'), ('G', 'f'), ('G', 'G'), ('f', 'f'), ('f', 'G'), ('G', 'G')]

无限迭代器

1. count(start,step):此迭代器从“ start”编号开始打印并无限打印。如果提到步长,则使用步长进行渐进式打印,否则默认步长为1。
范例:

iterator.count(5,2) prints -- 5,7,9,11...infinitely

2. cycle(iterable):此迭代器从传递的容器中按顺序打印所有值。当以循环方式打印所有元素时,它将重新开始打印
范例:

iterator.cycle([1,2,3,4]) prints -- 1,2,3,4,1,2,3,4,1...infinitely

3. repeat(val,num):此迭代器重复打印传递的值无数次。如果num被传入,打印直到那个数字停止。

# Python代码,展示如何使用repeat()
# 导入"itertools"
import itertools
# 使用repeat()
print ("重复打印 : ")
print (list(itertools.repeat(25,4)))

输出:

重复打印 :
[25, 25, 25, 25]