Python |集1中的迭代器函数

额外津贴: Python中的迭代器 Python在其定义中还允许使用一些有趣且有用的迭代器函数来实现高效循环,并加快代码的执行速度。模块中有许多内置迭代器“ itertools “. 该模块实现了许多迭代器构建块。 一些有用的迭代器: 1.累积(国际热核实验堆,func) :-这个迭代器需要 两个参数,iterable target和每次迭代target中的值时要遵循的函数 .如果没有传递函数, 附加 默认情况下发生。如果输入iterable为空,则输出iterable也将为空。 2.链条(iter1、iter2….) :-此功能用于打印所有 可重复目标中的值 在辩论中一个接一个地提到。

null

Python3

# Python code to demonstrate the working of
# accumulate() and chain()
# importing "itertools" for iterator operations
import itertools
# importing "operator" for operator operations
import operator
# initializing list 1
li1 = [ 1 , 4 , 5 , 7 ]
# initializing list 2
li2 = [ 1 , 6 , 5 , 9 ]
# initializing list 3
li3 = [ 8 , 10 , 5 , 4 ]
# using accumulate()
# prints the successive summation of elements
print ( "The sum after each iteration is : " ,end = "")
print ( list (itertools.accumulate(li1)))
# using accumulate()
# prints the successive multiplication of elements
print ( "The product after each iteration is : " ,end = "")
print ( list (itertools.accumulate(li1,operator.mul)))
# using chain() to print all elements of lists
print ( "All values in mentioned chain are : " ,end = "")
print ( list (itertools.chain(li1,li2,li3)))


输出:

The sum after each iteration is : [1, 5, 10, 17]The product after each iteration is : [1, 4, 20, 140]All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]

3.链条。来自_iterable() :-此函数的实现类似于chain(),但这里的参数是 列表列表或任何其他iterable容器 . 4.压缩(iter,选择器) :-这个迭代器 有选择地选择要打印的值 从经过的集装箱里 根据布尔列表 值作为其他参数传递。争论 对应于布尔值的true被打印出来 否则全部跳过。

Python3

# Python code to demonstrate the working of
# chain.from_iterable() and compress()
# importing "itertools" for iterator operations
import itertools
# initializing list 1
li1 = [ 1 , 4 , 5 , 7 ]
# initializing list 2
li2 = [ 1 , 6 , 5 , 9 ]
# initializing list 3
li3 = [ 8 , 10 , 5 , 4 ]
# initializing list of list
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)))
# using compress() selectively print data values
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 ])))


输出:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]The compressed values in string are : ['G', 'F', 'G']

5.dropwhile(函数,序号) :-此迭代器仅开始打印字符 在func之后。in参数返回false 这是第一次。 6.filterfalse(函数,序号) :-顾名思义, 此迭代器只打印返回false的值 对于传递的函数。

Python3

# Python code to demonstrate the working of
# dropwhile() and filterfalse()
# importing "itertools" for iterator operations
import itertools
# initializing list
li = [ 2 , 4 , 5 , 7 , 8 ]
# using dropwhile() to start displaying after condition is false
print ( "The values after condition returns false : " ,end = "")
print ( list (itertools.dropwhile( lambda x : x % 2 = = 0 ,li)))
# using filterfalse() to print false values
print ( "The values that return false to function are : " ,end = "")
print ( list (itertools.filterfalse( lambda x : x % 2 = = 0 ,li)))


输出:

The values after condition returns false : [5, 7, 8]The values that return false to function are : [5, 7]

参考 : https://docs.python.org/dev/library/itertools.html

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享