Created
November 20, 2012 15:26
-
-
Save matthewcornell/4118588 to your computer and use it in GitHub Desktop.
python list cycle alternatives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# current: | |
def __init__(self, listVals): | |
self.listVals = listVals | |
self.currentIndex = -1 | |
def sample(self, ignoredValue): | |
self.currentIndex = self.currentIndex + 1 if self.currentIndex < len(self.listVals) - 1 else 0 | |
return self.listVals[self.currentIndex] | |
# % variation: | |
def sample(self, ignoredValue): | |
self.currentIndex = (self.currentIndex + 1) % len(self.listVals) | |
return self.listVals[self.currentIndex] | |
# itertools.cycle variation: | |
def __init__(self, listVals): | |
self.listValsCycle = itertools.cycle(listVals) | |
def sample(self, ignoredValue): | |
return next(self.listValsCycle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment