在一个数据框架内,我有一个包含不同学术文献摘要的变量。下面是前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']
发布于 2021-11-29 21:23:07
您可以使用嵌套列表理解:
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('.', '')。
发布于 2021-11-29 20:26:50
使用for..each循环来遍历元素,替换“。有个空间。把句子分开,把列表连在一起。
abstractwords = []
for sentence in abstract:
sentence = sentence.replace(".", " ")
abstractwords.extend(sentence.split())https://stackoverflow.com/questions/70160871
复制相似问题