天天看點

python 清單list保留小數點後n位

保留後4位 

a = [0.5013333559036255, 0.7313334345817566, 0.843999981880188, 0.8953333497047424, 0.9380000233650208, 0.9699999690055847, 0.9839999079704285, 0.9913332462310791, 0.9953333139419556]

b = [float('{:.4f}'.format(i)) for i in a]

           

output:

b=[0.5013, 0.23, 0.1127, 0.0513, 0.0427, 0.032, 0.014, 0.0073, 0.004]