2011-03-16 17 views
0

我正在嘗試編寫一個程序,其中有幾個條件。我想從令牌列表中排除單詞列表(det)。高達if len(W) <=8:,它的工作就像我想要的那樣。但是,我無法讓程序找到我的令牌列表中的任何單詞,並將它們從打印中排除。排除Python條件中的詞

這是我目前:

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

請使用Code按鈕 – 0xAX 2011-03-16 13:53:08

+0

請通過縮進4個空格格式化代碼(按工具欄上的「{}」按鈕),並使用真實選項卡/空格代替(tab)'。 – kennytm 2011-03-16 13:53:39

+0

什麼是tkV?你是否將發電機聲明分配給任何東西? – nmichaels 2011-03-16 13:58:13

回答

3

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

也isLower()最好是最後一次檢查。 – Odomontois 2011-03-16 16:10:42

+0

@Odomontois,你是對的,謝謝。 – eumiro 2011-03-18 09:05:35