0

I've got some code that looks like this:

    valid = set()
    for string in substrings:
       for line in dictionary_words:
           if string in line:
               valid.add(string)
    f.writelines(sorted(valid))

Both dictionary_words and substrings are currently lists.

After the substring is found inside any dictionary_words, it should just go ahead and move onto the next substring.

What is the best way of writing that?

1
  • 1
    @F.C.: why not post this as an answer, since it is exactly what the O.P. is asking for ? Commented Oct 8, 2011 at 16:59

4 Answers 4

2
valid = set()
for string in substrings:
  for line in dictionary_words:
      if string in line:
          valid.add(string)
          break
f.writelines(sorted(valid))

@F.C.: If you use continue instead of break, it will run the next iteration of the inner for-loop.

Sign up to request clarification or add additional context in comments.

6 Comments

Would using 'break' jump to the next "line in dictionary_words" or to the next "string in substrings"?
If there were a statement on the same indentation level as for line in ... after break, it would jump there. But as there is nothing, it would continue with the next string in substrings
@Srikar: I think making it a dictionary, and using dictionary_of_dictionary_words.has_key(string) would not work, as string in line does substring matching, and has_key does not. Or maybe I'm missing something.
That's awesome, thank you. And 'continue' would move to the next 'line in dictionary_words' I gather, is that correct?
Yes. Therefore not having any benefit, as it would not alter the flow of the program.
|
1

Why not try this -

    valid = set()
    for string in substrings:
       if dictionary_words.has_key(string):
           valid.add(string)
    f.writelines(sorted(valid))

There is no need for the extra for-loop inside the main for-loop. This has_key solves your issue of moving on to the next substring is string is not in dictionary_word.

hope this helps...

3 Comments

dictionary_words is a list, although I could probably make it a tuple.
make dictionary_words a dict(). since you are interested in lookup, it'll be a whole lot faster than a list or a tuple
dictionary_words was a list, although I could probably make it a dictionary. Sorry, that was obviously confusing in the question in hindsight.
1

The following (untested code) should be equivalent to your loops:

valid =  set(s for s in substrings for ln in dictionary_words if s in ln)

In Python 3.0 you could use a set comprehension:

valid = {s for s in substrings for ln in dictionary_words if s in ln}

Slightly more efficient:

valid =  set(s for s in substrings if any(s in ln for ln in dictionary_words))

1 Comment

No problem, the last line is actually more like what you are looking for, and is equivalent to adding the break statement.
0
[valid.add(my_string) for my_string in substrings for line in dictionary_words if my_string in line]
f.writelines(sorted(valid))

Using List Comprehensions would be faster using loops in the way you have implemented.

2 Comments

Thank you very much. Sadly, the loops were better than my last version of it, which was truly ugly. I'll read up on list comprehensions.
You can also write list comps as your traditional loops for better readability, (although I prefer the default list comp style)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.