Oleg Melnikov
Oleg Melnikov

Reputation: 3298

Find and replace multiple comma/space instances in a string, Python

I have a string with multiple successive instances of , (comma+space) that I want to replace with a single instance. Is there a clean way to do so? I suppose RegEx can be helpful.

A naive example:

s = 'a, b, , c, , , d, , , e, , , , , , , f

The desired output:

'a, b, c, d, e, f

Naturally, the text can change, so the search should be for successive instances of ,.

Upvotes: 7

Views: 10429

Answers (5)

Subham
Subham

Reputation: 411

One more solution: go through the combination of the list and the same list shifted by one (in other words, by the pairs of consecutive items) and select the second item from each pair where the first (previous) item differs from the second (next) item:

s = 'a. b. . c. . . d. . . e. . . . . . . f'
test = []
for i in s:
    if i != ' ':
        test.append(i)


res = [test[0]] + [y for x,y in zip(test, test[1:]) if x!=y]

for x in res:
    print(x, end='')
 

yields

a.b.c.d.e.f
[Program finished]

Upvotes: 0

Gold
Gold

Reputation: 26

s = ", , a, b, , c, , , d, , , e, , , ,  , , , f,,,,"
s = [o for o in s.replace(' ', '').split(',') if len(o)]
print (s)

Upvotes: 1

AkaKaras
AkaKaras

Reputation: 102

the simplest way for your problem would be:

>>> s = 'a, b, , c, , , d, , , e, , , , , , , f'
>>> s = [x for x in s if x.isalpha()]
>>> print(s)
['a', 'b', 'c', 'd', 'e', 'f']

then, use join()

>>> ', '.join(s)
'a, b, c, d, e, f'

do it in one line:

>>> s = ', '.join([x for x in s if x.isalpha()])
>>> s
'a, b, c, d, e, f'

Just figure other way:

>>> s = 'a, b, , c, , , d, , , e, , , , , , , f'
>>> s = s.split()  #split all ' '(<- space)
>>> s
['a,', 'b,', ',', 'c,', ',', ',', 'd,', ',', ',', 'e,', ',', ',', ',', ',', ',', ',', 'f']
>>> while ',' in s:
...     s.remove(',')
>>> s
['a,', 'b,', 'c,', 'd,', 'e,', 'f']
>>> ''.join(s)
'a,b,c,d,e,f'

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89574

You can use reduce:

>>> from functools import reduce
>>> reduce( (lambda x, y: x+', '+y if y else x), s.split(', '))

(Where x is the carry and y the item)

Upvotes: 2

Dušan Maďar
Dušan Maďar

Reputation: 9909

So the regular expression searches for two or more instances of , (comma + space) and then in sub function you replace it with only a single ,.

import re
pattern = re.compile(r'(,\s){2,}')

test_string = 'a, b, , c, , , d, , , e, , , , , , , f'
print re.sub(pattern, ', ', test_string)
>>> a, b, c, d, e, f

and without a regular expression (as @Casimir et Hippolyte suggested in comment)

test_string = 'a, b, , c, , , d, , , e, , , , , , , f'
test_string_parts = test_string.split(',')
test_string_parts = [part.strip() for part in test_string_parts if part != ' ']
print ', '.join(test_string_parts)
>>> a, b, c, d, e, f

Upvotes: 11

Related Questions