-1

Input:

list=["@stellar I loo#oo)oooovvvv;vveee my K!" , "rl I loo#os#"]

I want like this output:

"stellar I loooooooovvvvvveee my K", "rl I looos"

How can i delete some ascii characters in list? ( not only #,!,@,) )

Jim
  • 87
  • 1
  • 7
  • Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – wwii May 10 '20 at 16:22

3 Answers3

1

You can use regex:

import re

regex = re.compile('[^a-zA-Z\ ]')

my_strings = =["@stellar I loo#oo)oooovvvv;vveee my K!" , "rl I loo#os#"]

print([regex.sub("", s) for s in my_strings])
>>> ['stellar I loooooooovvvvvveee my K', 'rl I looos']

This will replace all non-letter characters by an empty string (therefore deleting it)

You can use re.compile('[^0-9a-zA-Z\ ]') if you want to keep numbers

ted
  • 13,596
  • 9
  • 65
  • 107
1

You can map that list, with regex replace.

import re

list=["@stellar I loo#oo)oooovvvv;vveee my K!" , "rl I loo#os#"]
result = list(map(lambda x: re.sub('[^A-Za-z0-9\s]', '', x), list))

print(result)
xana
  • 499
  • 3
  • 13
  • it works properly. i have a short question, if inputs are "I'have" or "I'll" output -> "ihave" or "ill" how can i pass like this word ? – Jim May 10 '20 at 16:32
  • 1
    You can add \' to regex pattern [^A-Za-z0-9\s\'] it will ignore apostrophe. – xana May 10 '20 at 16:38
1

see below:

my_list=["@stellar I loo#oo)oooovvvv;vveee my K!" , "rl I loo#os#"]
chars_to_remove = ['!','@']
for word in my_list:
    for char in word:
        if char in chars_to_remove:
            word.replace(char, '')
Adam
  • 2,820
  • 1
  • 13
  • 33