PUNCT_CHARS = { '(', ')', ',', ',', '、', ':', ':', '[', ']', '#'}
words = ['a', '#good', 'student']
for word in words.copy():
for char in PUNCT_CHARS:
if char in word:
words.remove(word)
break
print(words)
['a', 'student']
I want to remove words that contain punctuations. Can the 2nd for loop be replaced with an ‘any’ or ‘all’ function to make it more efficient?
>Solution :
Take advantage of your PUNCT_CHARS set to check if the sets of characters are disjoint:
out = [w for w in words if PUNCT_CHARS.isdisjoint(w)]
Output: ['a', 'student']
To modify your original object:
words[:] = [w for w in words if PUNCT_CHARS.isdisjoint(w)]