alfasheep123
alfasheep123

Reputation: 13

string of numbers into a numpy list?

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.

Upvotes: 1

Views: 297

Answers (1)

mozway
mozway

Reputation: 260780

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

Upvotes: 1

Related Questions