views:

69

answers:

3

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.

A: 

you could try to use the itertools module and espacially the dropwhile function.

Mermoz
From my understanding of dropwhile, it would change the data in the list I use it on. Or perhaps I really don't understand what it does.
Erik
A: 

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.

John Pirie
I'm not sure I understand why you are using this as a dictionary. In particular mapping it to the value of 1.I had thought about checking if word is present in the list in some fashion, just was hoping for something a little more elegant I guess.Thanks for your suggestion, I very well may end up using it.
Erik
You could also use a set instead of dict.
Peter Milley
A: 

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.

Peter Milley
From experiments I just ran with your proposed method, it seems to mess up the enumerate that I forgot to mention I was using. I really liked your suggestion though, but I had left you working with incomplete information.
Erik
I just realized I was an idiot. I just realized I could set the iterator equal to the iterator that enumerate returns, and call next on that. Thanks for your suggestion!
Erik
Erik: see the above edit as well.
Peter Milley