2

I have a numpy 2D-array of string with shape (3,2):

ar_2d = array([['123', '456'],
               ['789', '0ab'],
               ['cde', 'fgh']],
              dtype='<U3')

To make it easier, I am sure the length of each string is equal.

And I have a function, i.e. namely split(), to make string '123' to python list ['1','2','3']

Now I would like to produce a 3D-array with '123' to an array array(['1', '2', '3']) and finally I can get a 3D-array with shape (3,2,3):

ar_3d = array([[['1', '2', '3'],
                ['4', '5', '6']],

                [['7', '8', '9'],
                 ['0', 'a', 'b']],

                [['c', 'd', 'e'],
                 ['f', 'g', 'h']]],
               dtype='<U1')

I have an idea that splitting the string to list first and write to file with numpy's format. Then, I shall read the array from the file.

If the elements are integer numbers, would it be easier? i.e. number 123 to list [1,2,3]

So here is the problem, is there an elegant method to do the convertion?

Thanks in advance!

2
  • Did the posted solution work for you? More info on what to do with solutions - stackoverflow.com/help/someone-answers Commented Jan 17, 2018 at 5:08
  • @Divakar Your solution is the very best one! Thank you again. Commented Jan 18, 2018 at 5:58

1 Answer 1

2

Use ndarray.view to view as U1 and then reshape into 3D -

In [15]: a
Out[15]: 
array([['123', '456'],
       ['789', '0ab'],
       ['cde', 'fgh']], 
      dtype='<U3')

In [16]: a.view('U1').reshape(a.shape + (-1,))
Out[16]: 
array([[['1', '2', '3'],
        ['4', '5', '6']],

       [['7', '8', '9'],
        ['0', 'a', 'b']],

       [['c', 'd', 'e'],
        ['f', 'g', 'h']]], 
      dtype='<U1')

The output would simply be a view of the input and hence this would be memory-efficient. As such, the runtime would be constant (irrespective of array shape) -

In [20]: %timeit a.view('U1').reshape(a.shape + (-1,))
1000000 loops, best of 3: 828 ns per loop

In [21]: a_big = np.tile(a,10000)

In [22]: %timeit a_big.view('U1').reshape(a_big.shape + (-1,))
1000000 loops, best of 3: 851 ns per loop
Sign up to request clarification or add additional context in comments.

Comments

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.