繁体   English   中英

在Python中排除条件中的单词

[英]Excluding words in Conditions in Python

我目前正在尝试编写一个有多个条件的程序。 我想从标记列表中排除单词列表(det)。 直到if len(W) <=8:它的工作正如我想要的那样。 但是,我无法让该程序在我的令牌列表中找到det中的任何单词,并将其从打印中排除。

这是我目前拥有的:

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.'

您的det似乎无效(请检查引号)。

如果要经常检查元素是否在列表中,可以使用set() ,它可以更快地检查内容。

整体看起来像这样:

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.'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM