IteratorChain

Class IteratorChain<E>

An IteratorChain is an Iterator that wraps a number of Iterators.

This class makes multiple iterators look like one to the caller. When any method from the Iterator interface is called, the IteratorChain will delegate(选派某人做某事) to a single underlying Iterator. The IteratorChain will invoke the Iterators in sequence until all Iterators are exhausted(用完).

Under many circumstances, linking Iterators together in this manner is more efficient (and convenient) than reading out the contents of each Iterator into a List and creating a new Iterator.

Calling a method that adds new Iterator after a method in the Iterator interface has been called will result in an UnsupportedOperationException.

NOTE: As from version 3.0, the IteratorChain may contain no iterators. In this case the class will function as an empty iterator.

NOTE: As from version 4.0, the IteratorChain stores the iterators in a queue and removes any reference to them as soon as they are not used anymore. Thus the methods setIterator(Iterator) and getIterators() have been removed and size() will return the number of remaining iterators in the queue.

Since:
2.1

在这里插入图片描述

【Python】Chaining multiple iterators together

The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().

Note: For more information, refer to Python Itertools

chain() function

It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output. Its output cannot be used directly and thus explicitly converted into iterables. This function come under the category iterators terminating iterators.

Syntax : chain (*iterables)

The iternal working of chain can be implemented as given below :

def chain(*iterables):
     for it in iterables:
       for each in it:
           yield each
Example 1: The odd numbers and even numbers are in separate lists. Combine them to form a new single list.
from itertools import chain

# a list of odd numbers
odd = [1, 3, 5, 7, 9]

# a list of even numbers
even = [2, 4, 6, 8, 10]

# chaining odd and even numbers
numbers = list(chain(odd, even))

print(numbers)

# [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Example 2: Some of the consonants are in a list. The vowels are given in a list. Combine them and also sort them.
from itertools import chain

# some consonants
consonants = ['d', 'f', 'k', 'l', 'n', 'p']

# some vowels
vowels = ['a', 'e', 'i', 'o', 'u']

# resultatnt list
res = list(chain(consonants, vowels))

# sorting the list
res.sort()

print(res)

# ['a', 'd', 'e', 'f', 'i', 'k', 'l', 'n', 'o', 'p', 'u']
Example 3: In the example below, Each String is considered to be an iterable and each character in it is considered to be an element in the iterator. Thus every character is yielded
from itertools import chain

res = list(chain('ABC', 'DEF', 'GHI', 'JKL'))

print(res)
# ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
Example 4:
from itertools import chain

st1 = "Geeks"
st2 = "for"
st3 = "Geeks"

res = list(chain(st1, st2, st3))
print("before joining :", res)

ans = ''.join(res)
print("After joining :", ans)
# before joining : [‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’] 
# After joining : GeeksforGeeks
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值