【问题标题】:Python: Cut off the last word of a sentence?Python:截断句子的最后一个单词?
【发布时间】:2011-09-10 03:18:36
【问题描述】:

从文本块中分割最后一个单词的最佳方法是什么?

我能想到

  1. 将其拆分为一个列表(按空格)并删除最后一项,然后重新连接该列表。
  2. 使用正则表达式替换最后一个单词。

我目前正在采用方法#1,但我不知道如何连接列表...

content = content[position-1:position+249] # Content
words = string.split(content, ' ')
words = words[len[words] -1] # Cut of the last word

非常感谢任何代码示例。

【问题讨论】:

    标签: python split concatenation word text-segmentation


    【解决方案1】:

    实际上,您不需要拆分所有单词。您可以使用rsplit 将文本按最后一个空格符号拆分为两部分。

    一些例子:

    >>> text = 'Python: Cut of the last word of a sentence?'
    >>> text.rsplit(' ', 1)[0]
    'Python: Cut of the last word of a'
    

    rsplit 是“反向拆分”的简写,与常规的 split 不同,它从字符串的末尾开始工作。第二个参数是要进行的最大拆分数 - 例如1 的值将为您提供两个元素列表作为结果(因为进行了一次拆分,导致输入字符串分为两段)。

    【讨论】:

    • 如果觉得有必要注意 rsplit 是反向拆分(不是正则表达式拆分),而 1 是 maxsplit,还有其他一些答案。
    • 只是一个需要注意的边缘情况。如果句子中只有一个单词,则此解决方案不会删除任何内容。
    • 如果您恰好想要字符串中的最后一个单词,text.rsplit(' ', 1)[-1]
    【解决方案2】:

    您绝对应该拆分然后删除最后一个单词,因为正则表达式会带来更多的复杂性和不必要的开销。您可以使用更多 Pythonic 代码(假设内容是字符串):

    ' '.join(content.split(' ')[:-1])
    

    这会将内容拆分为单词,获取除最后一个单词之外的所有单词,然后用空格重新连接单词。

    【讨论】:

      【解决方案3】:

      如果你喜欢紧凑:

      ' '.join(content.split(' ')[:-1]) + ' ...'
      

      【讨论】:

        【解决方案4】:

        如果您想保留当前方法,请使用' '.join(words) 连接列表。

        您可能还想用words = words[:-1] 替换words = words[len[words -1] 以利用列表切片。

        【讨论】:

          【解决方案5】:

          import re
          
          print ' '.join(re.findall(r'\b\w+\b', text)[:-1])
          

          【讨论】:

          • 我猜如果你的单词不仅被空格分割,那么正则表达式会给你带来好处。否则 rsplit 是您的选择。
          【解决方案6】:

          ' '.join(words) 会将列表重新组合在一起。

          【讨论】:

            【解决方案7】:

            获取空间的最后一个索引并拼接字符串

            >>> text = 'Python: Cut of the last word of a sentence?'
            >>> text[:text.rfind(' ')]
            'Python: Cut of the last word of a'
            

            【讨论】:

            • 这个在小字符串上比接受的答案快 15-20%。
            【解决方案8】:
                    
            def replace_ending(sentence, old, new):
                S1 = sentence
                O1 = old
                N1 = new
                # Check if the old string is at the end of the sentence 
                if O1 in S1:
                    # Using i as the slicing index, combine the part
                    # of the sentence up to the matched string at the 
                    # end with the new string
                    i = S1.rsplit(' ',1)[0] + str(" ") + N1     
                    new_sentence = i
                    return new_sentence
            
                # Return the original sentence if there is no match 
                return sentence
                
            print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
            # Should display "It's raining cats and dogs"
            
            

            【讨论】:

            • 我还在苦苦挣扎,还是个新手,但我确实让它工作了。希望对一些人有所帮助,谢谢。我添加了变量 S1 O1 N1 以使其更具可读性,因为它们不在原始问题上。
            【解决方案9】:

            另一种变体是使用参数“args*”

            例如:

            def truncate_sentences(length, *sentences):
              for sentence in sentences:
                print(sentence[:length])
            
            #call function
            
            truncate_sentences(8, "What's going on here", "Looks like we've been cut off")
            

            会输出:

            "What's g"
            "Looks li"
            

            让我们分解一下:

            1. 我们的函数truncate_sentences() 定义了两个参数。第一个是length 参数,它将指定我们要保留多少个字符。第二个是一个名为sentences 的参数,它与解包运算符配对,表示它将采用可变数量的参数。
            2. 在函数的每次迭代中,我们循环通过sentences 参数创建的元组(因为它与解包运算符配对)并根据提供的length 参数对句子执行切片。这会强制缩短 sentences 元组中的每个值的长度。

            【讨论】:

              【解决方案10】:

              试试下面,

              def replace_ending(sentence, old, new):
              # Check if the old string is at the end of the sentence 
              if sentence.endswith(old):
                  # Using i as the slicing index, combine the part
                  # of the sentence up to the matched string at the 
                  # end with the new string
                  i = sentence.rsplit(' ',1)[0] + str(" ")
                  new_sentence = i + new
                  return new_sentence
              
              # Return the original sentence if there is no match 
              return sentence
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-08-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-03-10
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多