Here is another solution:
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub('[^a-zA-Z0-9]+', '', _) for _ in my_list]
output:
['on3', 'two', 'three']
Answer from Mahesh Karia on Stack OverflowHere is another solution:
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub('[^a-zA-Z0-9]+', '', _) for _ in my_list]
output:
['on3', 'two', 'three']
Use the str.translate() method to apply the same translation table to all strings:
removetable = str.maketrans('', '', '@#%')
out_list = [s.translate(removetable) for s in my_list]
The str.maketrans() static method is a helpful tool to produce the translation map; the first two arguments are empty strings because you are not replacing characters, only removing. The third string holds all characters you want to remove.
Demo:
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans('', '', '@#%')
>>> [s.translate(removetable) for s in my_list]
['on3', 'two', 'three']
Regular expressions can be used to create a simple tokenizer and normalizer:
from __future__ import annotations
import re
def tokens(text: str) -> list(str):
"List all the word tokens in a text."
return re.findall('[\w]+', text.lower())
assert tokens("To be, or not to be, that is the question:") == ['to', 'be', 'or', 'not', 'to', 'be', 'that', 'is', 'the', 'question']
Otherwise, use an established library like spaCy to generate a list of tokens.
This can be achieved by Regular Expresiions
import re
modified_string = re.sub(r'\W+', '', input_string) # on individual tokens
modified_string = re.sub(r'[^a-zA-Z0-9_\s]+', '', input_string) # on sentence itself.Here I have modified RegEx to include spaces as well
'\W == [^a-zA-Z0-9_], so everything except numbers,alphabets and _ would be replaced by space
>>> lst=['hold', 'summit', 'septemb', '8', '9', '.', "'s", 'nancy-amelia', 'sydney', '.', 'energy', ',']
>>> import re
>>> list(filter(lambda x:x, map(lambda x:re.sub(r'[^A-Za-z]', '', x), lst)))
['hold', 'summit', 'septemb', 's', 'nancyamelia', 'sydney', 'energy']
>>>
try this
words = ['qwety', 'dot', 's', '"']
filter_words = ['"', 'dot']
filtered = [word for word in words if word not in filter_words]
(In line 3) We use python "list comprehension" with "if condition" in it.
That way you can create new list with values, that will pass through if condition (if word not in filter_words).
If you want to remove all non-letters chars, try:
words = ["".join(filter(lambda c: c.isalpha(), word)) for word in words]
print(words)
You can use built in shortcuts rather than have to specify all of the special characters. Here's a way to remove everything but "word characters":
import re
inp = ['rt', '@', 'dark', 'money', 'has', 'played', 'a', '#significant', 'role', 'in', 'tRhe', 'overall', 'increase', 'of', 'election', 'spending', 'in', 'state', 'judicial', 'elections.', 'https://e85zq', 'rt', '@', 'notice,', 'women,', 'how', 'you', 'are', 'always', 'the', 'target', 'of', 'democrats’', 'fear', 'mongering', 'in', 'an', 'election', 'year', 'or', 'scotus', 'confirmation.', 'it', 'is', 'not', 'because', 'our', 'rights', 'are', 'actually', 'at', 'risk.', 'it', 'is', 'because', 'we', 'are', 'easily', 'manipulated.', 'goes', 'allll', 'the', 'way', 'back', 'to', 'eve.', 'resist', 'hysteria', '&', 'think.', 'rt', '@', 'oct', '5:', 'last', 'day', 'to', 'register', 'to', 'vote.', 'oct', '13:', 'early', 'voting', 'starts.', 'oct', '23:', 'last', 'day', 'to', 'request', 'a', 'mail-in', 'ballot.', 'nov', '3:', 'election', 'day', 'rt', '@']
outp = [re.sub(r"[^A-Za-z]+", '', s) for s in inp]
print(outp)
Result:
['rt', '', 'dark', 'money', 'has', 'played', 'a', 'significant', 'role', 'in', 'tRhe', 'overall', 'increase', 'of', 'election', 'spending', 'in', 'state', 'judicial', 'elections', 'httpse85zq', 'rt', '', 'notice', 'women', 'how', 'you', 'are', 'always', 'the', 'target', 'of', 'democrats', 'fear', 'mongering', 'in', 'an', 'election', 'year', 'or', 'scotus', 'confirmation', 'it', 'is', 'not', 'because', 'our', 'rights', 'are', 'actually', 'at', 'risk', 'it', 'is', 'because', 'we', 'are', 'easily', 'manipulated', 'goes', 'allll', 'the', 'way', 'back', 'to', 'eve', 'resist', 'hysteria', 'amp', 'think', 'rt', '', 'oct', '5', 'last', 'day', 'to', 'register', 'to', 'vote', 'oct', '13', 'early', 'voting', 'starts', 'oct', '23', 'last', 'day', 'to', 'request', 'a', 'mailin', 'ballot', 'nov', '3', 'election', 'day', 'rt', '']
The ^ character here means match everything NOT mentioned in the set of characters that follow inside a [] pair. \w means "word characters"
. So the whole thing says "match everything but word characters." The nice thing about using a regular expression is that you can get arbitrarily precise as to just which characters you want to include or exclude.
No need to slice the result with [:100 to print it. Just print it as is, like I do. I assume that by using 100, you're wanting to make sure you go to the end of the list. The better way to do that is to just leave that component blank. So [:] means "take a slice of the string that is the full string", and [5:] means "take from the 6th character to the end of the string".
UPDATE: I just noticed that you said you don't want numbers in the result. So then I guess you just want letters. I changed the expression to do that. This is what's nice about a regular expression. You can tweak just what gets replaced without adding additional calls, loops, etc. but rather just change a string value.
Add those charecters to the regex as
[re.sub('[^a-zA-Z0-9$#%]+', '', _)for _ in a]
^^^
as @DYZ mentioned, you could also use '[^\w$#%]+' regex
[re.sub('[^\w$#%]+', '', _)for _ in a]
UPDATE-1
import re
a = ["on@3", "two#", "thre%e"]
special_char_to_be_removed = "%" # here you can change the values
regex = '[^\w{your_regex}]+'.format(your_regex=special_char_to_be_removed)
[re.sub(regex, '', _)for _ in a] Just add the list of characters to the list.
import re
a = ["on@3", "two$", "thre%e"]
final_list = [re.sub('[^a-zA-Z0-9\$#%]+', '', _) for _ in a]
print final_list
outputs
['on3', 'two$', 'thre%e']
$ has a meaning in regular expressions so you need to escape it with a \
If you want to take user input, just use
import re
a = ["on@3", "two$", "thre%e"]
except_special_chars = input('Exceptions:')
final_list = [re.sub('[^a-zA-Z0-9'+str(except_special_chars)+']+', '', _) for _ in a]
print final_list
then the user input the special characters between quotes ' and with an escaping \ if necessary.