Python 2.7.5错误打印浮点数列表 [英] Python 2.7.5 error printing list of float numbers

查看:119
本文介绍了Python 2.7.5错误打印浮点数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从此处回答一个问题(从两个列表中减去两个项目).最初的问题有两个带有浮点值的列表,目标是将它们压缩并减去

I was trying to answer a question from here (Substracion of two items from two lists). The original problem has two different lists with float values and the goal is to zip them and substract

此代码可以正常工作:

Enter=[7.12, 7.14, 7.24, 7.45, 7.28, 7.31, 7.18, 7.25, 7.33, 7.38]
Leave=[7.56, 7.24, 7.48, 7.52, 7.45, 7.57, 7.22, 7.31, 7.37, 7.41]
intervals = map(lambda x, y: y-x, Enter, Leave)

然后:

print intervals

输出:

[0.4399999999999995, 0.10000000000000053, 0.24000000000000021, 0.069999999999999396, 0.16999999999999993, 0.26000000000000068, 0.040000000000000036, 0.059999999999999609, 0.040000000000000036, 0.030000000000000249]

对我来说似乎真的很奇怪.并尝试了这个:

Which seemed really weird for me. And tried this:

print intervals[0]

输出:

0.44

最后,

for i in intervals:
    print i

输出:

0.44
0.1
0.24
0.07
0.17
0.26
0.04
0.06
0.04
0.03


问题是.为什么在打印整个列表时不打印浮点数,但打印一个正确的值呢?


So the question is. Why is printing floating values one by one print the correct value, while printing entire list doesnt?

请注意,我正在使用Live Sympy对其进行测试( Live Sympy ,不要没有在这里安装python),该程序运行的是Python 2.7.5

Just as a note, I'm using Live Sympy for testing it (Live Sympy, don't have python installed here) which runs Python 2.7.5

推荐答案

print方法格式化浮点数,但是当您打印列表时,它并没有格式化其内部值并按原样打印.

The print method formats the float however when you print the list it doesn't format its internal values and prints as it is.

    >>> print(intervals[0])   <== formatting is done by print here
    0.44
    >>> intervals[0]
    0.4399999999999995

    >>> print (type(intervals))
    <type 'list'>
    >>> print (type(intervals[0]))
    <type 'float'>

这篇关于Python 2.7.5错误打印浮点数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆