【问题标题】:Counting occurrences in string column计算字符串列中的出现次数
【发布时间】:2022-01-06 16:00:50
【问题描述】:

在数据框中,我有一个包含不同学术文献摘要的变量。您可以在下面找到前 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']

【问题讨论】:

    标签: python string split word


    【解决方案1】:

    您可以使用嵌套列表推导:

    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('.', '')

    【讨论】:

      【解决方案2】:

      使用 for..each 循环遍历元素,替换“.”带空格。拆分句子,并连接列表。

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

      【讨论】:

        猜你喜欢
        • 2020-12-10
        • 2019-06-03
        • 1970-01-01
        • 2014-04-24
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        • 2018-04-04
        相关资源
        最近更新 更多