Split Strings into words with multiple word boundary delimiters

前端 未结 30 2759
既然无缘
既然无缘 2020-11-21 05:09

I think what I want to do is a fairly common task but I\'ve found no reference on the web. I have text with punctuation, and I want a list of the words.

\"H         


        
30条回答
  •  故里飘歌
    2020-11-21 05:56

    Create a function that takes as input two strings (the source string to be split and the splitlist string of delimiters) and outputs a list of split words:

    def split_string(source, splitlist):
        output = []  # output list of cleaned words
        atsplit = True
        for char in source:
            if char in splitlist:
                atsplit = True
            else:
                if atsplit:
                    output.append(char)  # append new word after split
                    atsplit = False
                else: 
                    output[-1] = output[-1] + char  # continue copying characters until next split
        return output
    

提交回复
热议问题