2016-02-27 105 views
-1

刪除標記我有這種形式的嵌套列表:從嵌套列表

[[u' (SBAR - TMP (WHADVP-1 (WRB When)) (S (NP-SBJ (PRP it)))'] 
[u'(NP-SBJ (DT the) (NNS traders))'] 
[u'(NP (NNS orders) (S (-NONE- *ICH*-2)))'] 
[u'(PP-MNR (IN via) (NP (NNS computers)))'] 
[u'(S-2\n (NP-SBJ (-NONE- *))\n (VP\n (TO to)]] 

我想刪除的標籤和此輸出:

((when it)(the traders)(orders)(via computers)) 

誰能告訴我該怎麼做的蟒蛇?

+1

你到目前爲止試過了什麼?有沒有指示什麼是你想要的'標籤'與'單詞'?括號內的任何特定_結構?我想嘗試將它們轉換爲元組來提取特定的部分。 – aneroid

+0

您的代碼清單中似乎存在語法錯誤:最後一個unicode字符串未終止。 (可能只是在'']''''之前缺少一個'']部分。) –

回答

0

你可以得到一切不是大寫的。我不知道你在考慮什麼標籤,所以你可以從以下幾行開始:

import re 

arr = [[u' (SBAR - TMP (WHADVP-1 (WRB When)) (S (NP-SBJ (PRP it)))'], 
    [u'(NP-SBJ (DT the) (NNS traders))'], 
    [u'(NP (NNS orders) (S (-NONE- *ICH*-2)))'], 
    [u'(PP-MNR (IN via) (NP (NNS computers)))'], 
    [u'(S-2\n (NP-SBJ (-NONE- *))\n (VP\n (TO to)']] 

res = [' '.join(re.findall(r'(\b[A-Za-z][a-z ]+\b)', s[0])) for s in arr] 

print(res) 
# [u'When it', u'the traders', u'orders', u'via computers', u'to'] 
+0

感謝您的回答。 – Kalai