For 1D arrays, Numpy has a function called fromstring
, so it can be done very efficiently without extra libraries.
Briefly you can parse your string like this:
s = '[0 1 2 3]'
a = np.fromstring(s[1:-1], dtype=np.int, sep=' ')
print(a) # [0 1 2 3]
For nD arrays, one can use .replace()
to remove the brackets and .reshape()
to reshape to desired shape, or use Merlin's solution.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…