我的列表包含转换为 float 的值:
time = [1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]
但是当我执行 time[0]
时,该值会四舍五入到小数点后两位,如 1472120400.11
。访问单个列表值时如何保留原始值(最多 3 位小数以进行精确计算)?
我的代码:
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
newlst.append(float(time))
print newlst
print newlst[0]
输出:
[1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633, 1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]
1472120400.11
最佳答案
列表本身没有四舍五入的值。这是显示舍入的默认“打印”:
>>> time = [1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]
>>> time[0]
1472120400.107
>>> print time[0]
1472120400.11
您可以通过打印 repr 获得全精度输出相反:
>>> print repr(time[0])
1472120400.107
关于python - 当访问浮点值列表中的单个元素时,值会四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436802/