Python中的chain()函数可以用来组合多个迭代器,它的使用方法很简单,只需要在chain()函数中传入多个迭代器即可。
使用方法
需要定义多个迭代器,比如:
list1 = [1, 2, 3] list2 = [4, 5, 6] list3 = [7, 8, 9]
可以使用chain()函数将多个迭代器组合起来:
from itertools import chain iterators = chain(list1, list2, list3)
可以使用for循环遍历组合后的迭代器:
for i in iterators: print(i)
输出结果如下:
1 2 3 4 5 6 7 8 9
以上就是,只要按照上面的步骤操作,就可以轻松实现多个迭代器的组合。