python - 当访问浮点值列表中的单个元素时,值会四舍五入

标签 python python-2.7

我的列表包含转换为 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/

相关文章:

python - 黑色背景上的 IPython qtconsole 内联绘图无法正常工作

python - 通过 SOCKs 代理请求

用于提取字段的 Python 一行代码

python - 没有名为 'zope.deprecation' 的模块和简单的 hello world Pyramid 应用程序

python-2.7 - 如何在 Savitzky-Golay 滤波器中使用不确定性来加权残差。

python - Nose 工具中的 assert_raises() 并没有真正起作用

Python NumPy - 3D 数组的角度切片

Python 快速从 int 到 string 的转换

python-2.7 - CentOS 6.7、python distutils 和该死的 brp-python-bytecompile

python - 使用 argv 将参数传递给 python 中的函数