📜  Python中的迭代器函数 |设置 1

📅  最后修改于: 2022-05-13 01:55:00.537000             🧑  作者: Mango

Python中的迭代器函数 |设置 1

Perquisite: Python中的迭代器
Python在其定义中还允许一些有趣且有用的迭代器函数来有效循环并加快代码的执行速度。模块“ itertools ”中有许多内置迭代器。
该模块实现了许多迭代器构建块。
一些有用的迭代器:
1.accumulate(iter, func) :- 这个迭代器有两个参数,可迭代的目标和在目标值的每次迭代中将遵循的函数。如果没有传递函数,则默认进行加法。如果输入迭代为空,则输出迭代也将为空。
2.chain(iter1, iter2..) :- 该函数用于打印参数中提到的可迭代目标中的所有值

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)))


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])))


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 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. chain.from_iterable() :- 这个函数的实现方式与 chain() 类似,但这里的参数是列表列表或任何其他可迭代容器
4. compress(iter, selector) :- 这个迭代器根据作为其他参数传递的布尔列表选择性地从传递的容器中选择要打印的值。与 boolean 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, seq) :- 此迭代器仅在 func 之后开始打印字符。 in 参数第一次返回 false
6. filterfalse(func, seq) :- 顾名思义,这个迭代器只打印为传递的函数返回 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