我试图从这里回答一个问题(Substracion of two items from two lists)。原始问题有两个不同的浮点值列表,目标是压缩它们并减去
此代码可以正常工作:
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]
这对我来说似乎很奇怪。并尝试了这个:
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
所以问题是。为什么打印浮动值逐个打印正确的值,而打印整个列表不是?
就像我要注意的那样,我使用Live Sympy进行测试(Live Sympy,不要在这里安装python)运行Python 2.7.5
答案 0 :(得分:2)
print方法格式化浮动,但是当您打印列表时,它不会格式化其内部值并按原样打印。
>>> 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'>