Geek406
Geek406

Reputation: 31

How to find unknown character from a list of strings?

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?

Upvotes: 0

Views: 1015

Answers (2)

Red
Red

Reputation: 27577

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.

Upvotes: 0

Z Li
Z Li

Reputation: 4318

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

Upvotes: 2

Related Questions