Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy fromstring empty string separator

When I call:

np.fromstring('3 3 3 0', sep=' ')

it returns

array([ 3.,  3.,  3.,  0.])

Since by default, sep='', I would expect the following call to return the same result:

np.fromstring('3330')

However it raises

ValueError: string size must be a multiple of element size

Why is this? What's the most pythonic way of getting array([ 3., 3., 3., 0.]) from '3330'?

like image 431
user2561747 Avatar asked Sep 19 '25 02:09

user2561747


1 Answers

You could use np.fromiter:

In [11]: np.fromiter('3300', dtype=np.float64)
Out[11]: array([ 3.,  3.,  0.,  0.])

In [12]: np.fromiter('3300', dtype=np.float64, count=4)  # count=len(..)
Out[12]: array([ 3.,  3.,  0.,  0.])
like image 76
Andy Hayden Avatar answered Sep 20 '25 16:09

Andy Hayden