Excluding words in Conditions in Python
I'm currently trying to write a program where I have several conditions. I wanted to exclude a list of words (det) from a list of tokens. Up to if len(W) <=8:
, it worked just as I wanted it to. However, I could not get the program to find any of words in det in my list of tokens, 开发者_如何学运维and exclude them from the printing.
This is what I have currently:
det = ['the','a',an','\'s']
w for w in tkV if w not in det
def BT_pos1(w):
for w in tkV:
if w.islower():
if len(w) >=3:
if len(w) <=8:
if w not in det:
print w, ' may be a bt.'
Your det
seems to be invalid (check the quotes).
If you want to check often whether an element is in a list, you can use a set()
, which is much faster to check for content.
The whole could look like this:
det = set(["the", "a", "an", "'s"])
for w in tkV:
if 3 <= len(w) <= 8 and w not in det and w.islower():
print w, ' may be a bt.'
精彩评论