1

The string package is useful for stripping punctuation from individual strings as demonstrated below:

import string
stripPunct = str.maketrans('', '', string.punctuation)

word = 'foo.bar.baz'

word.translate(stripPunct)

Output: 'foobarbaz'

But what is the method to apply this exact same method to every string in a numpy array of strings?

myArr =   np.array(['foo.bar.baz', 'foo.bar.baz', 'foo.bar.baz'], dtype='<U15')


myArr.translate(stripPunct)
AttributeError: 'numpy.ndarray' object has no attribute 'translate'
6
  • Apologies, I edited the question Commented Sep 3, 2020 at 21:14
  • Is np.array -> list -> map or list comprehension -> np.array an option for you? Commented Sep 3, 2020 at 21:16
  • yes those are good suggestions Commented Sep 3, 2020 at 21:17
  • Also have a look at this post Commented Sep 3, 2020 at 21:19
  • Iterate? res = [words.translate(stripPunct) for words in myArr] Commented Sep 3, 2020 at 21:19

2 Answers 2

2
import string
import numpy as np

stripPunct = str.maketrans('', '', string.punctuation)

myArr =  np.array(['foo.bar.baz', 'foo.bar.baz', 'foo.bar.baz'])
# works for 'any.string.inputted'
new = np.array([i.translate(stripPunct) for i in myArr])

Output:

array(['foobarbaz', 'foobarbaz', 'foobarbaz'])
Sign up to request clarification or add additional context in comments.

Comments

1

You can use np.vectorize to make a vectorized function.


stripPunct=str.maketrans('', '', string.punctuation)
vecTrans=np.vectorize(lambda x:x.translate(stripPunct))
myArr=np.array(['foo.bar.baz', 'foo.bar.baz', 'foo.bar.baz'], dtype='<U15')

vecTrans(myArr)

>>>return: array(['foobarbaz', 'foobarbaz', 'foobarbaz'], dtype='<U9')

1 Comment

Is that any better than the list comprehension suggestion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.