在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Python/ 去掉小括號(hào)中的內(nèi)容?

去掉小括號(hào)中的內(nèi)容?

clipboard.png

想把每一行中 () 中的帶單詞的項(xiàng)去掉,例如第一行中紅色方框中的內(nèi)容都要全部去掉,其他行也是一樣。
怎么寫代碼實(shí)現(xiàn)?

( (S (CC But) (NP (NP (JJ other) (NNS sources)) (ADJP (RB close) (PP (TO to) (NP (DT the) (NN sale))))) (VP (VP (VBD said) (SBAR (S (NP (NNP Vivendi)) (VP (VBD was) (VP (VBG keeping) (S (NP (DT the) (NN door)) (ADJP (JJ open) (PP (TO to) (NP (JJ further) (NNS bids)))))))))) (CC and) (VP (VBD hoped) (S (VP (TO to) (VP (VB see) (S (NP (NNS bidders)) (ADJP (JJ interested) (PP (IN in) (NP (NP (JJ individual) (NNS assets)) (ADJP (NN team) (NN up.)))))))))))) )
( (S (CC But) (NP (NP (JJ other) (NNS sources)) (ADJP (RB close) (PP (TO to) (NP (DT the) (NN sale))))) (VP (VBD said) (SBAR (S (NP (NNP Vivendi)) (VP (VBD was) (VP (VBG keeping) (S (NP (DT the) (NN door)) (VP (VP (VB open) (PP (IN for) (NP (JJ further) (NNS bids))) (PP (IN in) (NP (DT the) (JJ next) (NN day)))) (CC or) (VP (VB two.)))))))))) )
回答
編輯回答
尕筱澄

測(cè)資:

string = '( (S (CC But) (NP (NP (JJ other) (NNS sources)) (ADJP (RB close) (PP (TO to) (NP (DT the) (NN sale))))) (VP (VBD said) (SBAR (S (NP (NNP Vivendi)) (VP (VBD was) (VP (VBG keeping) (S (NP (DT the) (NN door)) (VP (VP (VB open) (PP (IN for) (NP (JJ further) (NNS bids))) (PP (IN in) (NP (DT the) (JJ next) (NN day)))) (CC or) (VP (VB two.)))))))))) )'

代碼:

import re

result = re.sub('\(\w+ \w+\)', '',string)
print(result)

結(jié)果:

'( (S  (NP (NP  ) (ADJP  (PP  (NP  )))) (VP  (SBAR (S (NP ) (VP  (VP  (S (NP  ) (VP (VP  (PP  (NP  )) (PP  (NP   )))  (VP (VB two.)))))))))) )'

我回答過(guò)的問(wèn)題: Python-QA

2017年12月26日 02:49