views:

874

answers:

5

Suppose this is the string:

The   fox jumped   over    the log.

It would result in:

The fox jumped over the log.

What is the simplest, 1-2 liner that can do this? Without splitting and going into lists...

+5  A: 
>>> import re
>>> re.sub(' +',' ','The     quick brown    fox')
'The quick brown fox'
jleedev
This solution only handles single space characters. It wouldn't replace a tab or other whitespace characters handled by \s like in nsr81's solution.
Taylor Leese
That's true, `string.split` also handles all kinds of whitespaces.
jleedev
+9  A: 
import re
s = "The   fox jumped   over    the log."
re.sub("\s\s+" , " ", s)
nsr81
I'd tend to change that regex to `r"\s\s+"` so that it doesn't try to replace already-single spaces.
Ben Blank
updated. thanks for pointing that out.
nsr81
If you wanted that behavior, why not just `"\s{2,}"` instead of a workaround for not knowing moderately-advanced regex behavior?
Chris Lutz
+8  A: 

foo is your string:

" ".join(foo.split())
Taylor Leese
nsr81
You mean `split` not `strip` right?
Chris Lutz
sorry, meant split. it's a typo.
Taylor Leese
“Without splitting and going into lists...”
Gumbo
I ignored "Without splitting and going into lists..." because I still think it's the best answer.
Taylor Leese
+4  A: 

Similar to the previous solutions, but more specific: replace two or more spaces with one:

>>> import re
>>> s = "The   fox jumped   over    the log."
>>> re.sub('\s{2,}', ' ', s)
'The fox jumped over the log.'
Peter
+4  A: 

Have to agree with Paul McGuire's comment above. To me,

         ' '.join(the_string.split())

is vastly preferable to whipping out a regex. My measurements (Linux, Python 2.5) show the split-then-join to be almost 5 times faster than doing the "re.sub(...)", and still 3 times faster if you precompile the regex once and do the operation multiple times. And it is by any measure easier to understand -- much more pythonic.

Kevin Little