Can I skip an indeterminate amount of steps for an enclosing loop? (Python)
Question
This is perhaps a result of bad design, but here it goes. I wasn't quite sure how to explain this problem.
So I have code that iterates over a list of words. (This list does not change.) The code then parses and combines certain words together, depending on a set of criteria, storing them in a new list. The master loop, which is taking each word one at a time, would then need to skip what the code decided was a fit. So for example:
Master loop's list of words:
ListA = [apple, banana, penguin]
Within the master loop, let's say my code decided apple and bananna belong together, so
ListB = [apple banana, penguin]
Now I would like to have Master Loop skip over bananna, it doesn't need to run the check over whether to see if banana pairs with something else. So I would use a continue statement. Here is the problem. I don't know how many words will end up paired. So I could end up needing one continue, or three continues. The only way I can think of to run continue as many times as needed would be to use a loop...but that creates a problem since continue would affect the loop it is within.
Is there a way for me to make the master loop continue as many times as needed? Perhaps I am missing something simple. Thanks for your help.
EDIT
word_list = ["apple", "banana", "penguin"] #word_list will be arbitrary in practice
phrase_length = 0 #phrase_length is the amount of times I would like to skip
for k, word in enumerate(word_list):
#I would like to have the continues run here, before any of the below code
#the code down here decides what to pair in a forward fashion
#so it starts from apple and looks ahead to see if something fits with it
#continues in this manner till it comes up with the longest possible pairing
#phrase_length is then set equal to the amount of words used make the pairing
It would waste a considerable amount of computing time, if it had to execute the code for banana as well, checking forward from there as well. Which is why I would want to skip over the banana check.
Solution
You could explicitly use the next
method of the iterator.
>>> l = [1, 2, 3, 4, 5, 6]
>>> l_iter = iter(l)
>>> for n in l_iter:
if n==2:
print '{0}+{1}+{2}'.format(n, l_iter.next(), l_iter.next())
else:
print n
1
2+3+4
5
6
EDIT: yeah, that would get messy when combined with enumerate. Another option that comes to mind: write the function as a generator, something like:
def combined_words(word_list):
combined_word = ""
for k, word in enumerate(word_list):
combined_word += k
if next_word_can_be_paired:
pass
else: # next word can't be paired
yield combined_word
combined_word = ""
if combined_word:
yield combined_word # just in case anything is left over
Then call list(combined_words(word_list))
to get the new list.
OTHER TIPS
you could try to use the itertools module and espacially the dropwhile function.
Am I missing something?
word_list = ["apple", "banana", "penguin"]
skip_list = {}
for word in self.word_list:
if word in skip_list:
continue
# Do word-pairing logic; if word paired, skip_list[word] = 1
May not be the most programmatically efficient, but at least clear and concise.