我有一个由0和1组成的字符串,就像'00101'.我想将它转换为numpy数组numpy.array([0,0,1,0,1].
我正在使用for循环:
import numpy as np
X = np.zeros((1,5),int)
S = '00101'
for i in xrange(5):
X[0][i] = int(S[i])
Run Code Online (Sandbox Code Playgroud)
但由于我有很多字符串,每个字符串的长度是1024,这种方式非常慢.有没有更好的方法来做到这一点?