Рекомендую сделать Population
функцией генератора.См. Ключевое слово Python yield объяснено :
def Populate(text, c):
for i in range(c):
# compute variation
# [...]
yield variation
Создайте итератор и используйте next()
для получения следующего варианта в цикле, чтобы вы могли напечатать каждыйодин вариант:
populate_iter = Populate(text, 1000)
final_variation = None
while not done:
next_variation = next(populate_iter, None)
if next_variation :
final_variation = next_variation
# print current variation
# [...]
else:
done = True
Редактировать в соответствии с комментарием:
Чтобы не усложнять свой вопрос, я не упомянул, что Population
, это класс [...]
Конечно, Populate can be a class
тоже.В этом случае вы должны реализовать метод object.__iter__(self)
.Например:
class Populate:
def __init__(self, text, c):
self.text = text
self.c = c
def __iter__(self):
for i in range(self.c):
# compute variation
# [...]
yield variation
Создать итератор с помощью iter()
.например:
populate_iter = iter(Populate(text, 1000))
final_variation = None
while not done:
next_variation = next(populate_iter, None)
if next_variation :
final_variation = next_variation
# print current variation
# [...]
else:
done = True