I have an iterator that yields a dictionary with several data fields. Is there a way to split this stream of data into two following iterators that each feature only a certain data field of the preceding streamed dictionary?
class Splitter(IteratorBase):
def __init__(self, iterable):
super().__init__(iterable)
def __iter__(self):
for pt in self.iterable:
yield pt["field1"], pt["field2"]
does not work, because this just yields tuples with both fields
BaseIterator
, changing in__init__
to haveself.iterable = iterable
, and giving data as[{"field1": 1, "field2": 2}, {"field1": 3, "field2": 4}]
. A simplefoo = Splitter(data)
andfor a, b in foo:
works fine.itertools.tee
does, though the tuple iterator is only accessible between thetee
iterators.)