在一個資料框中,我有一個包含不同學術文獻摘要的變數。下面是前 3 個觀察結果的示例:
abstract = ['Word embeddings are an active topic in the NLP', 'We propose a new shared task for tactical data', 'We evaluate a semantic parser based on a character']
我想將這個變數中的句子拆分成單獨的單詞并洗掉可能的句點 '.'
本例中的代碼行應回傳以下串列:
abstractwords = ['Word', 'embeddings', 'are', 'an', 'active', 'topic', 'in', 'the', 'NPL', 'We', 'Propose', 'a', 'new', 'shared', 'task', 'for', 'tactical', 'data', 'We', 'evaluate', 'a', 'semantic', 'parser', 'based', 'on', 'a', 'character']
uj5u.com熱心網友回復:
您可以使用嵌套串列理解:
abstract = ['Word embeddings are an active topic in the NLP.', 'We propose a new shared task for tactical data.', 'We evaluate a semantic parser based on a character.']
words = [word.strip('.') for sentence in abstract for word in sentence.split()]
print(words)
# ['Word', 'embeddings', 'are', 'an', 'active', 'topic', 'in', 'the', 'NLP', 'We', 'propose', 'a', 'new', 'shared', 'task', 'for', 'tactical', 'data', 'We', 'evaluate', 'a', 'semantic', 'parser', 'based', 'on', 'a', 'character']
如果您還想洗掉'.'單詞中間的內容,請word.replace('.', '')改用。
uj5u.com熱心網友回復:
使用 for..each 回圈遍歷元素,替換“.” 有空間。拆分句子,并連接串列。
abstractwords = []
for sentence in abstract:
sentence = sentence.replace(".", " ")
abstractwords.extend(sentence.split())
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/370921.html
