Hi Kevin, kevin parks wrote: > I am trying to write a loop that iterates over a sequence and do > something x number of times. But sometimes i will need more events > (larger number x) than i have have items in the sequence, so if i need > more events that i have stuff in my sequence i would like to have the > loop reload and shuffle the deck and start all over again, reloading as > many times as necessary to get the number of events needed. I suppose i > could go around cyclically modulo the list size but i would want to > shuffle the deck before doing that again... >
Why not save the iterable it in the generator, then just reuse it? You can copy the iterable like this: import random def cycle(iterable): saved = list(iterable) #notice how I saved into a list, for I could not shuffle a TUPLE! while True: for element in saved: yield element random.shuffle(saved) def test(): seq = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten') loop = cycle(seq) count = 1 for item in range(25): print count, loop.next() count = count + 1 #you could use enumerate() instead of manually making the loop here. #for i, v in enumerate(loop): # print i, v Hope that helps, Hugo if __name__ == '__main__': test() _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor