0

I'm writing a program to find keywords line by line from a file in program. A piece of my code reproduced below is used to add case insensitive keywords (keywords are in a list L) to a list, seen, in order to produce only unique keywords, and add to the count of keywords I have. The code is as follows:

    for words in line:
        if (words.upper() or words.lower() in L) and (not in seen): # this means a keyword was found
            seen.append(words) # add keyword to the seen list to only find unique keywords
            count += 1 # add to count of keywords in this line

However when I try to run it gives me a syntax error with my if statement and highlights the "in" from "not in seen". What is wrong with my if statement?

Thanks.

Hazim
  • 381
  • 1
  • 7
  • 24

1 Answers1

2

You're not specifying what is not in seen. Your condition should be in the form of X not in Y. Also, your first expression doesn't do what you think it does: words.upper() or words.lower() in L checks if either words.upper() is not an empty string, or if words.lower() is in L.

You probably want this:

for words in line:
    if (words.upper() in L or words.lower() in L) and (words.upper() not in seen and words.lower() not in seen):
        seen.append(words)
        count +=1

If you don't care about the case of the words stored in seen, you could just transform all the words into one case (upper or lower), making your code much simpler:

for words in line:
    words = words.lower()
    if words in L and words not in seen:
        seen.append(words)
        count +=1
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • I can't believe how simple that was. Finally, if I put words.upper() or words.lower() in parenthesis for my first condition in L, would that work, or do I need to split it up like you did? – Hazim Nov 23 '14 at 14:42
  • You need to split them up. If you did what you suggested, `words.upper() or words.lower()` would evaluate to just `words.upper()`. – DanielGibbs Nov 23 '14 at 14:45