views:

78

answers:

3

I have a list of tags:

>>> tags_list
['tag1', 'second tag', 'third longer tag']

How can I replace whitespaces within each element of the list with "+" ? I was trying to do this with regular expressions but each string remains unchanged:

for tag in tags_list:
    re.sub("\s+" , " ", tag)

What is wrong with my approach ?

EDIT:

Yes I forgot to mention, that between the words in each tag we can have multiple whitespaces as well as tags can begin or end with whitespaces, since they're parsed from a comma separated string with split(",") : ("First tag, second tag, third tag"). Sorry for not being precise enough.

+6  A: 

re.sub() returns a new string, it does not modify its parameter in-place.

You want:

tags_list = [re.sub(r"\s+", "+", tag) for tag in tags_list]

Also, don't forget the r before the regex or you'll have to double all the backslashes inside (i.e. the regex you posted won't work).

EDIT: I assume replace whitespaces within each element of the list with "+" means collapsing all consecutive whitespace into a single +character. If not, use r"\s" instead of r"\s+".

Frédéric Hamidi
+2  A: 

What you were doing is not replacing spaces with +, you were replacing multiple whitespace characters with a single space, on top of that you were throwing results out. Here is what you need:

>>> tags = ['tag1', 'second tag', 'third longer tag']
>>> [re.sub(r'\s+', '+', i.strip()) for i in tags]
['tag1', 'second+tag', 'third+longer+tag']
>>> tags
['tag1', 'second tag', 'third longer tag']
SilentGhost
As @Axidos pointed out in a comment to my deleted answer, this does not work for consecutive spaces or tabs or newlines.
Space_C0wb0y
@Space: OP clearly mis-represents his actual task. If you look at his example, he doesn't deal with consecutive white spaces at all.
SilentGhost
A: 
>>> tags = ['tag1', 'second tag', 'third longer tag']
>>> [ '+'.join(i.split()) for i in tags ]
['tag1', 'second+tag', 'third+longer+tag']
ghostdog74