Array of numpy number for strings with removed zeros

question: my method of converting an array of numpy numbers to an array of numpy strings with a certain number of decimal places And did trailing zeros remove the “best” way?

import numpy as np
x = np.array([1.12345, 1.2, 0.1, 0, 1.230000])
print np.core.defchararray.rstrip(np.char.mod('%.4f', x), '0')

outputs:

['1.1235' '1.2' '0.1' '0.' '1.23']

what is the desired result. (I'm fine with the rounding issue)

Both the "rstrip" and "mod" functions are numpy functions, which means it's fast, but is there any way to do this with the ONE numpy built-in function? (i.e. does it have a “mod” parameter that I could not find?) It would save the overhead of duplicate copies, which is slow for very large arrays.

thank!

+4
source share
1 answer

. .

:

formatter = '%d'
if num_type == 'float':
  formatter = '%%.%df' % decimals
np.savetxt(out, arr, fmt=formatter)

out - , . , headers= np.savetxt. , .

numpy 1300 1300 , ( np.core.defchararray.rstrip(np.char.mod('%.4f', x), '0')), ~ 1,7 , np.savetxt 0,48 .

, np.savetxt - , .

: :

np.savetxt(out, arr, fmt='%.4g')

, , , .

+2

All Articles