There is a tuple:

a= ('!',',','?') 

And string

 dasd,sadarg!ada 

How can I find characters from a tuple in a string and delete them?

2 answers 2

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).

  • In vain you answer such questions. Solved the problem for the person, saved him from the thought processes. The task could be solved, stupidly, with the help of cycles, but then to think how to do it more beautifully and correctly. - Avernial
  • 2
    @Avernial is not an institute here :) everyone chooses how to learn. and help by example has never been superfluous. Any question can be resolved by yourself. By your logic, there is no need to answer? Or answer selectively? But why answer at all? There are docks, disassemblers ... and more :) - pavelip
  • Simply there are questions to which the answer is in the first language textbook. And then graduates who cannot find a substring in a string cannot find it) - Avernial
  • @Avernial: (feel the irony) the problem of homework is erased to holes in a large SO (summary: the answer should be complete and contain a complete working example of the code regardless of the author’s efforts) and it was also discussed in Russian Meta - jfs
  • 2
    @jfs, I don’t agree about clarity; for me, filtering is more intuitive than list comprehensions. Regarding the speed - you need to look for each case separately. In places that are not critical for speed, a relatively “slow” version with filter will do. In critical cases, it will probably make sense to write in C / C ++, and not in Python. - insolor

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() .