nag*_*sic 1 python string whitespace split
可能会拆分一个Python字符串(句子),以便它保留输出中单词之间的空格,但是在一个拆分子字符串中,通过在每个单词后面附加它?
例如:
given_string = 'This is my string!'
output = ['This ', 'is ', 'my ', 'string!']
Run Code Online (Sandbox Code Playgroud)
我大部分时间都避免使用正则表达式,但这里它非常简单:
import re
given_string = 'This is my string!'
res = re.findall(r'\w+\W?', given_string)
# res ['This ', 'is ', 'my ', 'string!']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94 次 |
| 最近记录: |