[英]How can I split a string in to multiple words, spaces and tabs in Python?
[英]How to split a string into words and spaces?
我想分割一个string = ['O little forests,']
以便输出包括单词和空格,最后一个逗号附加到最后一个单词。 这是所需的输出应该是什么样的:
output = ['O', ' ', 'little', ' ', 'forests,']
我能够将给定的字符串拆分为使用line.split()
排除空格的单词列表。 我欢迎你的建议!
你可以使用groupby :
from itertools import groupby
sentence = 'O little forests'
result = [''.join(v) for k, v in groupby(sentence, key=str.isspace)]
print(result)
产量
['O', ' ', 'little', ' ', 'forests']
你可以使用re(正则表达式):
import re
sentence = 'O little forests'
re.split("( )", sentence) # ['O', ' ', 'little', ' ', 'forests']
你可以用做这项工作re
像下面的代码。
import re
string = ['O little forests']
for a in string:
print(re.split(r'(\s+)', a))
输出:
['O', ' ', 'little', ' ', 'forests']
您可以在列表推导中在每个单词之间添加一个空格元素:
s = 'O little forest'
# puts ' ' element after each word.
st = [k for j in [[e, ' '] for e in s.split()] for k in j]
st.pop() # drop last.
st
>>> ['O', ' ', 'little', ' ', 'forest']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.