0

How would I delete an unknown character from a list of strings? For example my list is ['hi', 'h@w', 'are!', 'you;', '25'] and I want to delete all the characters that are not words or numbers?

How would I do this?

Red
  • 26,798
  • 7
  • 36
  • 58
Geek406
  • 21
  • 2
  • I think the word character could mean several different things. You may want to be more specific. – Chris Dec 08 '20 at 19:15
  • 1
    Does this answer your question? [Stripping everything but alphanumeric chars from a string in Python](https://stackoverflow.com/questions/1276764/stripping-everything-but-alphanumeric-chars-from-a-string-in-python) – Random Davis Dec 08 '20 at 19:16
  • Probably the cleanest way would be to use a `regex` to parse the string. You could also make an array of invalid characters and `replace` any in the string with an empty string – Rashid 'Lee' Ibrahim Dec 08 '20 at 19:16

2 Answers2

2

Regex:

s = ['hi', 'h@w', 'are!', 'you;', '25']
[re.sub(r'[^A-Za-z0-9 ]+', '', x) for x in s]
['hi', 'hw', 'are', 'you', '25']
Z Li
  • 4,133
  • 1
  • 4
  • 19
0

Use re.sub:

from re import sub
lst = ['hi', 'h@w', 'are!', 'you;', '25']
lst = [sub('[^\w]', '', i) for i in lst]
print(lst)

Output:

['hi', 'hw', 'are', 'you', '25']

Explanation:

This line: sub('[^\w]', '', i) tells python to replace all the substrings of pattern "[^\w]" with an empty string, "" inside the i string, and return the result.

The pattern [^\w] finds all the characters in a string that are not letters or numbers.

Red
  • 26,798
  • 7
  • 36
  • 58