Reputation: 381
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.
Upvotes: 0
Views: 4238
Reputation: 10191
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
Upvotes: 2