2015-05-09 41 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也 –

相关问题