Sorry if the title is poorly worded, I'm not sure how to express it. I have a function that basically iterates over the second dimension of a two-dimensional iterative. The following is simple playback:
words = ['ACGT', 'TCGA']
def make_lists():
for i in range(len(words[0])):
iter_ = iter([word[i] for word in words])
yield iter_
lists = list(make_lists())
for list_ in lists:
print(list(list_))
The execution of these outputs:
['A', 'T']
['C', 'C']
['G', 'G']
['T', 'A']
I would like to get generators instead of evaluating wordsin case it is wordsvery long, so I tried the following:
words = ['ACGT', 'TCGA']
def make_generators():
for i in range(len(words[0])):
gen = (word[i] for word in words)
yield gen
generators = list(make_iterator())
for gen in generators:
print(list(gen))
However, working outputs:
['T', 'A']
['T', 'A']
['T', 'A']
['T', 'A']
I don’t know exactly what is going on. I suspect that this has something to do with understanding the generator without covering its scope when they yield, so they all share. If I create generators inside a separate function and get a return from this function, it works.