Convert string to numpy array
list
may help you do that.
import numpy as np
mystr = "100110"
print np.array(list(mystr))
# ['1' '0' '0' '1' '1' '0']
If you want to get numbers instead of string:
print np.array(list(mystr), dtype=int)
# [1 0 0 1 1 0]
You could read them as ASCII characters then subtract 48 (the ASCII value of 0
). This should be the fastest way for large strings.
>>> np.fromstring("100110", np.int8) - 48
array([1, 0, 0, 1, 1, 0], dtype=int8)
Alternatively, you could convert the string to a list of integers first:
>>> np.array(map(int, "100110"))
array([1, 0, 0, 1, 1, 0])
Edit: I did some quick timing and the first method is over 100x faster than converting it to a list first.