2015-05-09 80 views
1
list1 = 'it is, a good,day' 

我需要用逗號將字符串的所有部分分開:'it is''a good''day',然後將'ouf'在各部分之間,因此它可以打印it is ouf a good ouf dayPython3如何單獨在truple項目

注昏迷後在', a good'之後的空間是否還有一種方法只能在逗號​​後刪除空格?

回答

4

您可以使用splitstripjoinmap

>>> list1 = 'it is, a good,day' 
>>> ' ouf '.join(map(str.strip,list1.split(','))) 
'it is ouf a good ouf day' 

或者你可以嘗試replace

>>> list1.replace(', ',',').replace(',',' ouf ') 
'it is ouf a good ouf day' 

考慮到編輯,我建議re模塊的sub功能。

>>> re.sub(r',\s?',' ouf ',list1) 
'it is ouf a good ouf day' 
+0

我非常喜歡re模塊!非常感謝你 –

+0

@bob歡迎您!祝一切順利。 –

+0

@bob在這裏接受http://stackoverflow.com/questions/29998421/extending-list-returns-none也 –

相關問題