Round off dict values to 2 decimals

前端 未结 6 1735
庸人自扰
庸人自扰 2021-01-16 11:29

I\'m having a hard time rounding off values in dicts. What I have is a list of dicts like this:

y = [{\'a\': 80.0, \'b\': 0.0786235, \'c\': 10.0, \'d\': 10.6         


        
6条回答
  •  再見小時候
    2021-01-16 11:59

    I know this question is old, but here is a quick one-liner solution that works at least here in Linux Python 2.7.6 and might be interesting to someone else:

    y = [{ x : round(z, 2) for x,z in yi.items()} for yi in y ]
    

    However, this might be inefficient for larger data sets, as it re-generates the list/dict structure.

提交回复
热议问题