我需要帮助将列表ty2
打印到2位小数。
ty2
是包含t
和y
值的表行列表。 t
和y
值有3位小数,我需要它们有2位小数。 ty2
值应为2位小数。我尝试过,但没有成功print = "%.2f" % (ty2)
print 'Store Data in a Nested List'
print '--------------------'
print 'Task a)' # Task Number
t = [0.000, 0.194, 0.387, 0.581, 0.775, 0.968, 1.162, 1.356, 1.549, 1.743, 1.937] # List created for t from ball_table2
y = [0.000, 1.656, 2.944, 3.864, 4.416, 4.600, 4.416, 3.864, 2.944, 1.656, 0.000] # List created for y from ball_table2
ty1 = [] # Empty List
ty2 = []
print ' Nested List (ty1)'
for t, y in zip (t, y):
ty1.append ([t, y])
ty2.append ([t, y])
import scitools. pprint2 # Imported Pretty Print options
scitools. pprint2.float_format = '% .2f ' # In two decimals
scitools. pprint2. pprint (ty1)
print '--------------------'
print 'Task b)'
print ty2
"""
Store Data in a Nested List
--------------------
Task a)
Nested List (ty1)
[[ 0.00 , 0.00 ],
[ 0.19 , 1.66 ],
[ 0.39 , 2.94 ],
[ 0.58 , 3.86 ],
[ 0.78 , 4.42 ],
[ 0.97 , 4.60 ],
[ 1.16 , 4.42 ],
[ 1.36 , 3.86 ],
[ 1.55 , 2.94 ],
[ 1.74 , 1.66 ],
[ 1.94 , 0.00 ]]
--------------------
Task b)
[[0.0, 0.0], [0.194, 1.656], [0.387, 2.944], [0.581, 3.864], [0.775, 4.416], [0.968, 4.6], [1.162, 4.416], [1.356, 3.864], [1.549, 2.944], [1.743, 1.656], [1.937, 0.0]]
“”“
答案 0 :(得分:1)
因此,如果您有一个包含值元组(t,y)的列表,则可以在此行中舍入到新的元组列表中:
rounded = [ ( round ( a [ 0 ], 2 ), round ( a [ 1 ], 2 )) for a in ty2 ]
答案 1 :(得分:0)
print ' Nested List (ty1)'
for t, y in zip (t, y):
t = '%.2f' % t
y = '%.2f' % y
ty1.append ([t, y])
ty2.append ([t, y])
我刚插入这个,这实际上解决了我的问题。感谢所有人的贡献< 3