8

I have a NumPy string array

['HD\,315', 'HD\,318' ...]

I need to replace every 'HD\,' to 'HD ', i.e. I want to get new array like below

 ['HD 315', 'HD 318' ...]

What is the SHORTEST way to solve this task in Python? Is it possible to do this without FOR loop?

drastega
  • 1,581
  • 5
  • 30
  • 42

3 Answers3

14

Use python list comprehension:

L = ['HD\,315', 'HD\,318' ]
print [s.replace('HD\,' , 'HD ') for s in L]

But it uses for

Alternatively you can use map():

print map(lambda s: s.replace('HD\,' , 'HD '), L)

for python3 use list(map(lambda s: s.replace('HD\,' , 'HD '), L))

famargar
  • 3,258
  • 6
  • 28
  • 44
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • If I had a list of values to replace e.g. ["HD\", "HA\", "AB\], what's the most efficient way to iterate using the list comprehension you stated? – Howeitzer Sep 04 '18 at 08:59
8

You can use the numpy.core.defchararray.replace function, which performs the for operation using numpy instead:

import numpy.core.defchararray as np_f

data = ['HD\,315', 'HD\,318']

new_data = np_f.replace(data, 'HD\,', 'HD')
user2750362
  • 382
  • 3
  • 6
5

Something along these lines will work if your strings are of fixed length, and your array is of type string, and not of type object. Note that it circumvents python for loops, such as encountered in a list comprehension, and is correspondingly much faster:

import numpy as np
data = np.array(['HD\,315', 'HD\,318'])

view = data.view(np.uint8).reshape(data.shape + (data.dtype.itemsize,))
view[:,2] = 32
print data

Of course if your commas may appear in various places, logical indexing would be required (ie, view[view==92] = 32). Even if your strings are not all exactly equal length, but at least of a short and bounded length, placing them in a fixed length array could speed things up a lot at the cost of some extra memory, if you have a lot of these strings. Note that numpy.char contains a lotof useful utility functions for vectorized string manipulations. Speaking of which...

np.char.replace(data, ',', ' ')
Eelco Hoogendoorn
  • 10,459
  • 1
  • 44
  • 42