How to convert a string to numpy array?

Converting string to numpy array

Given a string abcxyz, I want it to return a numpy array like:

array(["a", "b", "c", "x", "y", "z"]). 

I have tried fromstring

Bingrid = np.fromstring(elements, dtype=str) 

but it returns

ValueError: Zero-valued itemsize.

>Solution :

Use list().

import numpy as np
s = "abcxyz"
print(np.array(list(s)))

Output:

['a' 'b' 'c' 'x' 'y' 'z']

Leave a Reply