Efficiently split a string using multiple separators and retaining each separator?

前端 未结 9 1340
野趣味
野趣味 2021-02-02 10:44

I need to split strings of data using each character from string.punctuation and string.whitespace as a separator.

Furthermore, I need for the

9条回答
  •  鱼传尺愫
    2021-02-02 11:34

    For any arbitrary collection of separators:

    def separate(myStr, seps):
        answer = []
        temp = []
        for char in myStr:
            if char in seps:
                answer.append(''.join(temp))
                answer.append(char)
                temp = []
            else:
                temp.append(char)
        answer.append(''.join(temp))
        return answer
    
    In [4]: print separate("Now is the winter of our discontent", set(' '))
    ['Now', ' ', 'is', ' ', 'the', ' ', 'winter', ' ', 'of', ' ', 'our', ' ', 'discontent']
    
    In [5]: print separate("Now, really - it is the winter of our discontent", set(' ,-'))
    ['Now', ',', '', ' ', 'really', ' ', '', '-', '', ' ', 'it', ' ', 'is', ' ', 'the', ' ', 'winter', ' ', 'of', ' ', 'our', ' ', 'discontent']
    

    Hope this helps

提交回复
热议问题