Home > Mobile >  Convert string of nested list of digits without separator to 2D NumPy array
Convert string of nested list of digits without separator to 2D NumPy array

Time:12-29

I got a string of lists that looks like this:

'[[0123][1234][3456]]'

but I want to insert it to 2D NumPy array of ints.

The result should be [[0, 1, 2, 3], [1, 2, 3, 4], [3, 4, 5, 6]].

Is there an easy way to do it without a first to run over the string and every number cast it to an int and insert it to a list, and at the end insert it to the NumPy object?

CodePudding user response:

The straightforward solution you have already mentioned is to strip and split the input string using the [] brackets, create a nested list and turn it into a NumPy array:

s = '[[0123][1234][3456]]'
t = (
    s
    .strip('][')  # '0123][1234][3456'
    .split('][')  # ['0123', '1234', '3456']
)
a = np.array([[int(v) for v in u] for u in t])

You could use np.fromiter to replace the inner list comprehension:

a = np.array([np.fromiter(u, dtype=int) for u in t])

Using np.stack to combine the inner 1-d arrays works as well:

a = np.stack([np.fromiter(u, dtype=int) for u in t])

Similarly to Reconcile np.fromiter and multidimensional arrays in Python, you could first use np.fromiter to create a 1-d array containing all the numbers and afterwards reshape it into a 2-d array:

from itertools import chain

a = np.fromiter(chain.from_iterable(t), dtype=int).reshape(len(t), -1)

But I'm not sure if there would be any benefit of doing that.

  • Related