There is a tuple:
a= ('!',',','?') And string
dasd,sadarg!ada How can I find characters from a tuple in a string and delete them?
There is a tuple:
a= ('!',',','?') And string
dasd,sadarg!ada How can I find characters from a tuple in a string and delete them?
For example:
>>> a=('!',',','?') >>> s='dasd,sadarg!ada' >>> ''.join(filter(lambda x: x not in a, s)) 'dasdsadargada' or
>>> ''.join(x for x in s if x not in a) 'dasdsadargada' In this and in another case, we do not delete characters from the old string, but create a new string that includes only those characters of the old string that satisfy the condition "the character x does not belong to the tuple a" (x not in a).
The best way to strip punctuation from a string in Python shows how you can remove punctuation using different approaches such as regular expressions, str.replace() , bytes.translate() , listcomp / genexpr, and also compares their performance. A similar question Deleting consonants from a string in Python makes a comparison for Python 3. Remove punctuation from Unicode formatted strings demonstrate solutions with an emphasis on non-ascii punctuation.
The easiest solution to understand is to use genexpr as shown in the @insolor answer . An identical solution that uses listcomp is more productive on CPython :
punct = ('!',',','?') no_punct = ''.join([c for c in text if c not in punct]) If the input text contains only ascii, then the .translate() method is 10 to 20 times faster :
translation_table = dict.fromkeys(map(ord, punct)) def remove_punctuation(text): # Python 3.5+ improves performance otherwise use bytes.translate return text.translate(translation_table) To remove all possible Unicode punctuation from the text :
#!/usr/bin/env python3 import regex # $ pip install regex def remove_punctuation(text): return regex.sub(r"\p{P}+", "", text) The performance of a solution with .replace() with increasing punct and text length (a comparison with other approaches for each case is necessary):
from functools import reduce no_punct = reduce(lambda text, p: text.replace(p, ''), punct, text) For short tuples .replace() inferior (slightly) only bytes.translate() .
Source: https://ru.stackoverflow.com/questions/437188/
All Articles