【发布时间】:2021-08-22 09:13:19
【问题描述】:
我正在尝试在空格上拆分如下字符串:
string = "This is a test."
# desired output
# ['This', ' ', 'is', ' ', 'a', ' ', 'test.']
# actual output, which does make sense
result = string.split()
# ['This', 'is', 'a', 'test.']
还有 re.split 保留分隔符,但不是我希望的方式:
import re
string = "This is a test."
result = re.split(r"( )", string)
# ['This',
# ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ',
# 'is', ' ',
# 'a', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ', '', ' ',
# 'test.']
我可以这样做并达到我想要的结果:
string = "This is a test."
result = []
spaces = ''
word = ''
for letter in string:
if letter == ' ':
spaces += ' '
if word:
result.append(word)
word = ''
else:
word += letter
if spaces:
result.append(spaces)
spaces = ''
if spaces:
result.append(spaces)
if word:
result.append(word)
print(result)
# ['This', ' ', 'is', ' ', 'a', ' ', 'test.']
但这感觉不是最好的方法。有没有更 Pythonic 的方式来实现这一点?
【问题讨论】: