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

前端 未结 9 1339
野趣味
野趣味 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:28

    from itertools import chain, cycle, izip
    
    s = "Now is the winter of our discontent"
    words = s.split()
    
    wordsWithWhitespace = list( chain.from_iterable( izip( words, cycle([" "]) ) ) )
    # result : ['Now', ' ', 'is', ' ', 'the', ' ', 'winter', ' ', 'of', ' ', 'our', ' ', 'discontent', ' ']
    

提交回复
热议问题