0

I have a numpy array with 3 elements; from this array, I'd like to create a list where each element is in scientific notation, and not a string. This seems like it should be easy, but I'm having trouble with it. My current script is as follows:

import numpy as np

ary = np.array([2.124598e+00, 9.1542e+00, 1.049e+00]) 
ary = ary.astype(np.float)
ary = ary.tolist()
ary = ['%.9e' % x for x in ary]
print(ary)
print(type(ary))

Output is:

['2.124598000e+00', '9.154200000e+00', '1.049000000e+00']
<type 'list'>

How can I maintain scientific notation for each element, and have a non-string element type?

Edit: The goal here is to create an input file, so the formatting is done to match the formatting of a known input file. My ideal output is as follows:

[2.124598000e+00, 9.154200000e+00, 1.049000000e+00]
<type 'list'>

Removing ['%.9e' % x for x in ary] gives me the following, without the scientific notation that I'm looking for:

[2.124598, 9.1542, 1.049]
<type 'list'>
AaronJPung
  • 1,105
  • 1
  • 19
  • 35
  • 1
    Just wandering, why do you need this? If you are doing mathematical operations you can convert the result into scientific notation after you get it. – sbowde4 May 18 '16 at 19:27
  • @sbowde4 - I'm writing an input file, and I'm trying to match the formatting of an existing input file. – AaronJPung May 18 '16 at 19:28

1 Answers1

0

You're actually converting the float list, ary, to a String list when you do

ary = ['%.9e' % x for x in ary]

It would probably be best to carry forward the precision of the numpy float array in your further calculations and then format to the number of significant digits you need/want at the end for output.

Saif Charaniya
  • 131
  • 1
  • 8