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.
You're not specifying what is
not in seen. Your condition should be in the form ofX not in Y. Also, your first expression doesn't do what you think it does:words.upper() or words.lower() in Lchecks if eitherwords.upper()is not an empty string, or ifwords.lower()is inL.You probably want this:
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: