简体   繁体   English

创建元素列表,每个元素都有科学计数法

[英]Create a list of elements, with each element with scientific notation

I have a numpy array with 3 elements; 我有一个包含3个元素的numpy数组; 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: 如果删除['%.9e' % x for x in ary]['%.9e' % x for x in ary]得到以下结果,而没有我正在寻找的科学符号:

[2.124598, 9.1542, 1.049]
<type 'list'>

You're actually converting the float list, ary, to a String list when you do 您实际上是在执行以下操作时将浮动列表ary转换为字符串列表

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. 最好在以后的计算中保留numpy浮点数组的精度,然后将其格式化为最后需要/想要输出的有效位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM