我試圖得到這個結果:
input: "this is a example"
output: ["this", "is", "a", " ", "example"]
但是使用.split(" ")我得到這個:
output: ["this", "is", "a", "", "", "example"]
uj5u.com熱心網友回復:
使用re.findall我們可以嘗試交替匹配兩邊被空格包圍的單詞或空格:
inp = "this is a example"
parts = re.findall(r'\w |(?<=[ ])\s (?=[ ])', inp)
print(parts) # ['this', 'is', 'a', ' ', 'example']
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473937.html
標籤:python-3.x 细绳
下一篇:如何將以下語法轉換為es6語法?
