拆分字符串的最佳方法是什么?
text = "hello there how are you"用Python?
所以我最终会得到这样的数组:
['hello there', 'there how', 'how are', 'are you']我试过这样做:
liste = re.findall('((\S+\W*){'+str(2)+'})', text)
for a in liste:
print(a[0])但我得到了:
hello there
how are
you如何使findall函数在搜索时只移动一个令牌?
发布于 2019-07-11 23:16:17
另一种选择就是split,zip,join,就像.
sentence = "Hello there how are you"
words = sentence.split()
[' '.join(i) for i in zip(words, words[1:])]https://stackoverflow.com/questions/56998097
复制相似问题