简体   繁体   English

计算字符串列中的出现次数

[英]Counting occurrences in string column

Within a dataframe I have a variable containing different abstracts of academic literature.在 dataframe 中,我有一个包含不同学术文献摘要的变量。 Below you find a example of the first 3 observations:您可以在下面找到前 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']

I want to split the sentences in this variable in seperate words and remove possible periods '.'我想将这个变量中的句子分成单独的单词并删除可能的句点“。”

The line of code in this case should return the following list:这种情况下的代码行应返回以下列表:

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']

Use for..each loop to go through elements, replace "."使用 for..each 循环到 go 通过元素,替换“。” with a space.有一个空格。 Split the sentence, and concatenate the lists.拆分句子,并连接列表。

abstractwords = []
for sentence in abstract:
    sentence = sentence.replace(".", " ")
    abstractwords.extend(sentence.split())

You can use nested list comprehension:您可以使用嵌套列表推导:

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']

If you want to remove '.'如果要删除'.' in the middle of the words as well, use word.replace('.', '') instead.在单词的中间,使用word.replace('.', '')代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM