What is the pythonic way to remove trailing spaces from a string?

The function parameter satisfies the following rules:

  • It has no leading spaces.
  • This may have trailing spaces.
  • The line may have alternating spaces.

Purpose: Remove duplicate spaces that alternate and separate white spaces.

Here's how I do it now:

# toks - a priori no leading space
def squeeze(toks):
  import re
  p = re.compile(r'\W+')
  a = p.split( toks ) 
  for i in range(0, len(a)):
    if len(a[i]) == 0:
      del a[i]
  return ' '.join(a) 

>>> toks( '  Mary  Decker   is hot   ' )
Mary Decker is hot

Can this be improved? Pythonic enough?

+3
source share
6 answers

Here's how I do it:

" ".join(toks.split())

PS. Is there a subliminal message in this question ?; -)

+9
source

You can not use rstrip ()?

some_string.rstrip() 

or strip () to delete a line on both sides?

In addition, strip () methods also support the transfer of arbitrary strip characters:

string.strip = strip(s, chars=None)
    strip(s [,chars]) -> string

: : , .

API !

+4

:

, . .

>>> squeeze('x    !    y')
'x y' # oops

1: \W + ( ), \s + ( )

>>> toks = 'x  !  y  z  '
>>> re.split('\W+', toks)
['x', 'y', 'z', '']
>>> re.split('\s+', toks)
['x', '!', 'y', 'z', '']

2: , . , , , - . , re.split() . , , . , , :

if a and not a[-1]: # guard against empty list
    del a[-1]

, , ( , , re), , , ( , " " ) " ":

a = [x for x in p.split(toks) if x]

- a:

return ' '.join(x for x in p.split(toks) if x)

"Pythonic"... , , genxp :

return ' '.join(toks.split())
+2

, re, . :

def toks(s):
    return ' '.join([x for x in s.split(' ') if x])

... , split, join .

""? . .

; , Pythonic.

, , . , ( ), s.split(' ') s.split() - None split() .

+1

Pythonic, , Python a [i] deleting a[i] if a[i]=='', keeping a[i] if a[i]!=''.

,

def squeeze(toks):
    import re
    p = re.compile(r'\W+')
    a = p.split( toks )
    for i in range(0, len(a)):
        if len(a[i]) == 0:
            del a[i]
    return ' '.join(a)

def squeeze(toks):
    import re
    p = re.compile(r'\W+')
    a = p.split( toks )
    a = [x for x in a if x]
    return ' '.join(a)

def squeeze(toks):
    import re
    p = re.compile(r'\W+')
    return ' '.join([x for x in p.split( toks ) if x])

, , , :

def squeeze(toks):
    import re
    p = re.compile(r'\W+')
    return ' '.join((x for x in p.split( toks ) if x))

:

def squeeze(toks):
    import re
    p = re.compile(r'\W+')
    return ' '.join(x for x in p.split( toks ) if x)

.

.

, , Python , re squeeze() , ( , ), re defautlt:

import re
def squeeze(toks,re = re):
    p = re.compile(r'\W+')
    return ' '.join(x for x in p.split( toks ) if x)

:

import re
def squeeze(toks,p = re.compile(r'\W+')):
    return ' '.join(x for x in p.split( toks ) if x)

.

.

: if x , '' '', p.split( toks ), .

, , , :

import re
def squeeze(toks,p = re.compile(r'\w+')):
    return ' '.join(p.findall(toks))

.

.

, r'\W+' , .

, , ' ' , '\f' , '\n' , '\r' , '\t' , '\v' (.\s re), :

import re
def squeeze(toks,p = re.compile(r'\s+')):
    return ' '.join(x for x in  p.split( toks ) if x)

, :

import re
def squeeze(toks,p = re.compile(r'\S+')):
    return ' '.join(p.findall(toks))

, ' '.join(toks.split())

' ' '\t', ,

import re
def squeeze(toks,p = re.compile(r'[^ \t]+')):
    return ' '.join(p.findall(toks))

- .

0

, . ?

import re

result = '  Mary  Decker   is hot   '
print(f"=={result}==")

result = re.sub('\s+$', '', result)
print(f"=={result}==")

result = re.sub('^\s+', '', result)
print(f"=={result}==")

result = re.sub('\s+', ' ', result)
print(f"=={result}==")

==  Mary  Decker   is hot   ==
==  Mary  Decker   is hot==
==Mary  Decker   is hot==
==Mary Decker is hot==
0
source

All Articles