9

Having such list:

x = ['+5556', '-1539', '-99','+1500']

How can I remove + and - in nice way?

This works but I'm looking for more pythonic way.

x = ['+5556', '-1539', '-99', '+1500']
n = 0
for i in x:
    x[n] = i.replace('-','')
    n += 1
n = 0
for i in x:
    x[n] = i.replace('+','')
    n += 1
print x

Edit

+ and - are not always in leading position; they can be anywhere.

peyo
  • 351
  • 4
  • 15
user1768615
  • 281
  • 1
  • 5
  • 12

6 Answers6

20

Use string.translate(), or for Python 3.x str.translate:

Python 2.x:

>>> import string
>>> identity = string.maketrans("", "")
>>> "+5+3-2".translate(identity, "+-")
'532'
>>> x = ['+5556', '-1539', '-99', '+1500']
>>> x = [s.translate(identity, "+-") for s in x]
>>> x
['5556', '1539', '99', '1500']

Python 2.x unicode:

>>> u"+5+3-2".translate({ord(c): None for c in '+-'})
u'532'

Python 3.x version:

>>> no_plus_minus = str.maketrans("", "", "+-")
>>> "+5-3-2".translate(no_plus_minus)
'532'
>>> x = ['+5556', '-1539', '-99', '+1500']
>>> x = [s.translate(no_plus_minus) for s in x]
>>> x
['5556', '1539', '99', '1500']
Duncan
  • 92,073
  • 11
  • 122
  • 156
16

Use str.strip() or preferably str.lstrip():

In [1]: x = ['+5556', '-1539', '-99','+1500']

using list comprehension:

In [3]: [y.strip('+-') for y in x]
Out[3]: ['5556', '1539', '99', '1500']

using map():

In [2]: map(lambda x:x.strip('+-'),x)
Out[2]: ['5556', '1539', '99', '1500']

Edit:

Use the str.translate() based solution by @Duncan if you've + and - in between the numbers as well.

Community
  • 1
  • 1
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
12
x = [i.replace('-', "").replace('+', '') for i in x]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
2

string.translate() will only work on byte-string objects not unicode. I would use re.sub:

>>> import re
>>> x = ['+5556', '-1539', '-99','+1500', '45+34-12+']
>>> x = [re.sub('[+-]', '', item) for item in x]
>>> x
['5556', '1539', '99', '1500', '453412']
jfd
  • 1,151
  • 10
  • 10
  • You can still use `value.translate()` on Python 2 when `value` is a unicode object but you need to pass in a different translation map. I updated my answer to include this case. – Duncan Oct 04 '16 at 14:12
0

These functions clean a list of strings of undesired characters.

lst = ['+5556', '-1539', '-99','+1500']
to_be_removed = "+-"

def remove(elem, to_be_removed):
    """ Remove characters from string"""
    return "".join([char for char in elem if char not in to_be_removed])

def clean_str(lst, to_be_removed):
   """Clean list of strings"""
   return [remove(elem, to_be_removed) for elem in lst]

clean_str(lst, to_be_removed)
# ['5556', '1539', '99', '1500']
Enrique Pérez Herrero
  • 3,699
  • 2
  • 32
  • 33
-1
basestr ="HhEEeLLlOOFROlMTHEOTHERSIDEooEEEEEE"

def replacer (basestr, toBeRemove, newchar) :
    for i in toBeRemove :
        if i in basestr :
        basestr = basestr.replace(i, newchar)
    return basestr



newstring = replacer(basestr,['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'], "")

print(basestr)
print(newstring)

Output :

HhEEeLLlOOFROlMTHEOTHERSIDEooEEEEEE

helloo

G Locarso
  • 1
  • 1