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

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

    from string import punctuation, whitespace
    
    s = "..test. and stuff"
    
    f = lambda s, c: s + ' ' + c + ' ' if c in punctuation else s + c
    l =  sum([reduce(f, word).split() for word in s.split()], [])
    
    print l
    

提交回复
热议问题