我有这份清单
a = [['c', '1.3e-8', '4.5e-8'], ['h', '3.4e-5', '2.3e-7', '2.3e-5']]
我想格式化' e'字符串仅作为
a = [[ 'c', '0.000000013', '0.000000045'], ['h', '0.000034', '0.00000023', '0.000023']]
我怎样才能在Python中执行此操作?谢谢!
答案 0 :(得分:1)
如果你确定只有第一个元素不是浮点数。
from decimal import Decimal
a = [['c', '1.3e-8', '4.5e-8'], ['h', '3.4e-5', '2.3e-7', '2.3e-5']]
for inx, rec in enumerate(a):
a[inx] = [rec[0]] + ['{:.{precise}f}'.format(Decimal(val),
precise=int(val[-1])+1) for val in rec[1:]]
print(a)
输出:
[['c', '0.000000013', '0.000000045'], ['h', '0.000034', '0.00000023', '0.000023']]