Python 教程之控制流(13)终止迭代器

简介: Python 教程之控制流(13)终止迭代器

终止迭代器

终止迭代器用于处理短输入序列,并根据所用方法的功能生成输出。

不同类型的终止迭代器有:

  • 累加(iter, func): 此迭代器采用两个参数,可迭代目标和在 target 中值的每次迭代中将遵循的函数。如果未传递任何函数,则默认进行加法。如果输入可迭代为空,则输出可迭代也为空。
    例:

# 演示accumlate()工作原理的Python代码
import itertools
import operator
# 正在初始化列表1
li1 = [1, 4, 5, 7]
# 使用 accumlate()打印元素的连续求和
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))

输出:

The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
  • 链(iter1, iter2. 此函数用于打印其参数中提到的可迭代目标中的所有值。
    例:

# 演示和 chain()的工作原理的Python代码
import itertools
# 初始化 list 1
li1 = [1, 4, 5, 7]
# 初始化 list 2
li2 = [1, 6, 5, 9]
# 初始化 list 3
li3 = [8, 10, 5, 4]
# 使用chain()打印列表的所有元素
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain(li1, li2, li3)))

输出:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
  • chain.from_iterable(): 这个函数的实现方式与 chain() 类似,但这里的参数是列表列表或任何其他可迭代容器。
    例:

# 演示 chain.from_iterable() 工作的Python代码
import itertools
# 初始化 list 1
li1 = [1, 4, 5, 7]
# 初始化 list 2
li2 = [1, 6, 5, 9]
# 初始化 list 3
li3 = [8, 10, 5, 4]
# 初始化列表的列表
li4 = [li1, li2, li3]
# using chain.from_iterable() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain.from_iterable(li4)))

输出:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
  • 压缩(iter, 选择器): 此迭代器根据作为其他参数传递的布尔列表值,有选择地从传递的容器中选取要打印的值。将打印与布尔值 true 对应的参数,否则将跳过所有参数。
    例:

# 演示和compress() 的工作原理的Python代码
import itertools
# 使用compress() 有选择地打印数据值
print ("The compressed values in string are : ", end ="")
print (list(itertools.compress('GEEKSFORGEEKS', [1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])))

输出:

The compressed values in string are : ['G', 'F', 'G']
  • dropwhile(func, seq): 此迭代器仅在 func 之后开始打印字符。在参数中首次返回 false。
    例:

# 演示dropwhile() 工作原理的Python代码
import itertools
# 初始化 list
li = [2, 4, 5, 7, 8]
# 使用dropwhile() 在条件为false后开始显示
print ("The values after condition returns false : ", end ="")
print (list(itertools.dropwhile(lambda x : x % 2 == 0, li)))

输出:

The values after condition returns false : [5, 7, 8]
  • filterfalse(func, seq): 顾名思义,此迭代器仅打印为传递的函数返回 false 的值。
    例:

# 演示 filterfalse() 工作的Python代码
import itertools
# 初始化 list
li = [2, 4, 5, 7, 8]
# 使用filterfalse() 打印错误值
print ("The values that return false to function are : ", end ="")
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))

输出:

The values that return false to function are : [5, 7]
  • 是(可迭代、启动、停止、步进): 此迭代器有选择地打印作为参数传递的其可迭代容器中提到的值。此迭代器采用 4 个参数、可迭代容器、起始位置、结束位置和步骤。
    例:

# 演示islice() 工作的Python代码
import itertools
# 初始化 list
li = [2, 4, 5, 7, 8, 10, 20]
# 使用islice()根据需要对列表进行切片,从第二个索引开始打印,直到第六个,跳过2
print ("The sliced list values are : ", end ="")
print (list(itertools.islice(li, 1, 6, 2)))

输出:

The sliced list values are : [4, 7, 10]
  • 星图(功能,元组列表): 此迭代器将函数和元组列表作为参数,并根据列表的每个元组中的函数返回值。
    例:

# 演示starmap()工作的Python代码
import itertools
# 初始化元组列表
li = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10, 1) ]
# 使用starmap()作为选择值acc.to函数选择所有元组值中的min
print ("The values acc. to function are : ", end ="")
print (list(itertools.starmap(min, li)))

输出:

The values acc. to function are : [1, 1, 4, 1]
  • takewhile(func, 可迭代): 此迭代器与 dropwhile() 相反,它打印值,直到函数第一次返回 false。
    例:

# 演示takewhile() 工作原理的Python代码
import itertools
# 初始化列表
li = [2, 4, 6, 7, 8, 10, 20]
# 使用takewhile() 打印值,直到条件为false。
print ("The list values till 1st false value are : ", end ="")
print (list(itertools.takewhile(lambda x : x % 2 == 0, li )))

输出:

The list values till 1st false value are : [2, 4, 6]
  • **三通(迭代器,计数):-**此迭代器将容器拆分为参数中提到的许多迭代器。
    例:

# 演示tee() 工作的Python代码
import itertools
# 初始化 list
li = [2, 4, 6, 7, 8, 10, 20]
# 在迭代器中存储列表
iti = iter(li)
# 使用tee() 生成迭代器列表,可以生成具有相同值的3个迭代器的列表。
it = itertools.tee(iti, 3)
# 打印迭代器的值
print ("The iterators are : ")
for i in range (0, 3):
  print (list(it[i]))

输出:

The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
  • zip_longest(可迭代1,可迭代2,填充): 此迭代器按顺序交替打印可迭代对象的值。如果其中一个可迭代对象已完全打印,则其余值将由分配给 fillvalue 的值填充。
    例:

# 演示zip_longest() 工作原理的Python代码
import itertools
# 使用ziplongest() 组合两个iterable。
print ("The combined values of iterables is : ")
print (*(itertools.zip_longest('GesoGes', 'ekfrek', fillvalue ='_' )))

输出:

The combined values of iterables is  : 
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')


目录
相关文章
|
13天前
|
机器学习/深度学习 算法 搜索推荐
从理论到实践,Python算法复杂度分析一站式教程,助你轻松驾驭大数据挑战!
【10月更文挑战第4天】在大数据时代,算法效率至关重要。本文从理论入手,介绍时间复杂度和空间复杂度两个核心概念,并通过冒泡排序和快速排序的Python实现详细分析其复杂度。冒泡排序的时间复杂度为O(n^2),空间复杂度为O(1);快速排序平均时间复杂度为O(n log n),空间复杂度为O(log n)。文章还介绍了算法选择、分而治之及空间换时间等优化策略,帮助你在大数据挑战中游刃有余。
41 4
|
10天前
|
数据采集 Web App开发 数据可视化
Python爬虫教程:Selenium可视化爬虫的快速入门
Python爬虫教程:Selenium可视化爬虫的快速入门
|
19天前
|
监控 数据可视化 搜索推荐
【Python篇】matplotlib超详细教程-由入门到精通(下篇)2
【Python篇】matplotlib超详细教程-由入门到精通(下篇)
29 8
|
16天前
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
【10月更文挑战第1天】告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
45 4
|
19天前
|
存储 索引 Python
Python 迭代器是怎么实现的?
Python 迭代器是怎么实现的?
22 6
|
19天前
|
编解码 数据可视化 IDE
【Python篇】matplotlib超详细教程-由入门到精通(下篇)1
【Python篇】matplotlib超详细教程-由入门到精通(下篇)
26 3
|
18天前
|
存储 大数据 Python
Python 中迭代器与生成器:深度解析与实用指南
Python 中迭代器与生成器:深度解析与实用指南
12 0
|
19天前
|
数据可视化 IDE 开发工具
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
174 13
|
19天前
|
数据可视化 API 数据处理
【Python篇】matplotlib超详细教程-由入门到精通(上篇)
【Python篇】matplotlib超详细教程-由入门到精通(上篇)
59 5
|
19天前
|
数据可视化 IDE 开发者
【Python篇】PyQt5 超详细教程——由入门到精通(终篇)
【Python篇】PyQt5 超详细教程——由入门到精通(终篇)
23 1