-1

For example, if I had this list of invalid characters:

invalid_char_list = [',', '.', '!']

And this list of strings:

string_list = ['Hello,', 'world.', 'I', 'am', 'a', 'programmer!!']

I would want to get this new list:

new_string_list = ['Hello', 'world', 'I', 'am', 'a', 'programmer']

withouth , or . or ! in any of the strings in the list because those are the characters that are in my list of invalid characters.

Diego
  • 33
  • 3
  • To be clearer, what exactly have you tried so far? We're much more here to help with specific questions of the form "I tried X, but it did not do what I expect and instead resulted in an error!" accompanied by a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) .. that said, some shape of looping over the strings and removing unwanted chars is probably right! – ti7 Oct 30 '22 at 17:57

4 Answers4

1

You can use regex and create this pattern : [,.!] and replace with ''.

import re
re_invalid = re.compile(f"([{''.join(invalid_char_list)}])")
# re_invalid <-> re.compile(r'([,.!])', re.UNICODE)

new_string_list = [re_invalid.sub(r'', s) for s in string_list]
print(new_string_list)

Output:

['Hello', 'world', 'I', 'am', 'a', 'programmer']
  • [.,!] : Match only this characters (',', '.', '!') in the set
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • For OP. I think rather than an exclude list, it'll be a lot easier for you to define an allow list of characters rather than a exclude list. Invert the allow list to remove all characters you're not explicitly expecting. `re.sub(r"[^a-zA-Z0-9]", "", s)` – flakes Oct 30 '22 at 17:59
  • @flakes, thanks for your comment, But OP says, he/she has a list of invalid, so I think we should use that `list`, and for now that list have these chars : `, or . or !` but when we write `[^a-zA-Z0-9]` we remove all char except of these: `(a-zA-Z0-9)` – I'mahdi Oct 30 '22 at 18:01
  • That's why I upvoted you and only left this as a comment :) – flakes Oct 30 '22 at 18:01
0

You can try looping through the string_list and replacing each invalid char with an empty string.

invalid_char_list = [',', '.', '!']
string_list = ['Hello,', 'world.', 'I', 'am', 'a', 'programmer!!']

for invalid_char in invalid_char_list:
    string_list=[x.replace(invalid_char,'') for x in string_list]

print(string_list)

The Output:

['Hello', 'world', 'I', 'am', 'a', 'programmer']
E Joseph
  • 316
  • 2
  • 8
0

We can loop over each string in string_list and each invalid character and use String.replace to replace any invalid characters with '' (nothing).

invalid_char_list = [',', '.', '!']
   string_list = ['Hello,', 'world.', 'I', 'am', 'a', 'programmer!!']
   formatted_string_list = []
   for string in string_list:
       for invalid in invalid_char_list:
           string = string.replace(invalid, '')
       formatted_string_list.append(string)
terrenana
  • 33
  • 1
  • 5
0

You can use strip():

string_list = ['Hello,', ',world.?', 'I', 'am?', '!a,', 'programmer!!?']

new_string_list = [c.strip(',.!?') for c in string_list]

print(new_string_list)

#['Hello', 'world', 'I', 'am', 'a', 'programmer']
Arifa Chan
  • 947
  • 2
  • 6
  • 23