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'
?
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.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With