Re: [Tutor] cycle w/ shuffle
Thanks Kent. That is what i did the first time around but it didn't work ... but that was do to something else that was not working in the script hehe of course, this just shuffles the saved copy of the list which is egg-zactly what i needed "duh" ... sorry... gosh... -kevin-- On Apr 27, 2006, at 6:00 AM, [EMAIL PROTECTED] wrote: > kevin parks wrote: >> it seems to me that i need something like itertools cycle, except that >> i need to keep track of when i have exhausted my list and then call >> random.shuffle() on my sequence. >> >> def cycle(iterable): >> saved = [] >> for element in iterable: >> yield element >> saved.append(element) >> while saved: > random.shuffle(saved) ### >> for element in saved: >> yield element > > Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cycle w/ shuffle
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
Re: [Tutor] cycle w/ shuffle
kevin parks wrote: > it seems to me that i need something like itertools cycle, except that > i need to keep track of when i have exhausted my list and then call > random.shuffle() on my sequence. > > def cycle(iterable): > saved = [] > for element in iterable: > yield element > saved.append(element) > while saved: random.shuffle(saved) ### > for element in saved: > yield element Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cycle w/ shuffle
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... it seems to me that i need something like itertools cycle, except that i need to keep track of when i have exhausted my list and then call random.shuffle() on my sequence. def cycle(iterable): saved = [] for element in iterable: yield element saved.append(element) while saved: for element in saved: yield element 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 if __name__ == '__main__': test() here i have gone through my list 2.5 times .. and somewhere after the second and third(tho incomplete) rounds i need to shuffle... but i am not sure how to fix that. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor