Home > OS >  string of numbers into a numpy list?
string of numbers into a numpy list?

Time:10-09

I want to make '1 2 3' into a numpy [1, 2, 3]

I've tried

x = '1 2 3'
x = np.fromstring(x, dtype=int, sep=' ')

and it works, but is there any other way to do it? Maybe using np.array()? Thanks.

CodePudding user response:

If you really want you could do np.array(x.split()), but the numpy.fromstring method will be faster.

Timing for np.fromstring on 1000 times x: 45.2 µs ± 4.29 µs

Timing for np.array: 329 µs ± 67.6 µs

  • Related