-4

I have a numpy array of integers that look like this X000Y000. X and Y can be of length 1, 2, or 3 and might contain 0s. I want to transform every element in the array to just X. I feel like a regex could be used for this but cannot figure out a good one to use, or how to apply that to a whole array.

Example: 14000010000 should become 140.

Emma
  • 27,428
  • 11
  • 44
  • 69
Jake Guida
  • 21
  • 1
  • 5

1 Answers1

0

I assume X and Y can't begin with 0. [1-9]\d{0,2} matches a number from 1 to 3 digits that doesn't begin with 0.

So the regexp to extract X and Y should be:

^([1-9]\d{0,2})000([1-9]\d{0,2})000$

Then you can use re.sub() to remove the zeroes between X and Y.

regex = re.compile(r'^([1-9]\d{0,2})000([1-9]\d{0,2})000$');
i = 14000010000
istr = str(i)
new_i = int(regex.sub(r'\1\2', istr)

You can map this over your numpy array

regex = re.compile(r'^([1-9]\d{0,2})000([1-9]\d{0,2})000$');
new_array = np.fromiter((int(regex.sub(r'\1\2', str(x)) for x in array), array.dtype)

See Most efficient way to map function over numpy array for various ways of mapping a function over a numpy array.

Barmar
  • 741,623
  • 53
  • 500
  • 612