user1768615
user1768615

Reputation: 281

How can I remove multiple characters in a list?

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.

Upvotes: 9

Views: 60236

Answers (6)

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']

Upvotes: 0

G Locarso
G Locarso

Reputation: 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

Upvotes: -1

Duncan
Duncan

Reputation: 95632

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']

Upvotes: 20

jfd
jfd

Reputation: 1149

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']

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250891

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.

Upvotes: 16

Rakesh
Rakesh

Reputation: 82755

x = [i.replace('-', "").replace('+', '') for i in x]

Upvotes: 12

Related Questions