How to round each item in a list of floats to 2 decimal places?

前端 未结 6 2090
有刺的猬
有刺的猬 2021-01-31 07:20

I have a list which consists of float values but they\'re too detailed to proceed. I know we can shorten them by using the (\"%.f\" % variable) operator, like:

6条回答
  •  情书的邮戳
    2021-01-31 07:44

    If you really want an iterator-free solution, you can use numpy and its array round function.

    import numpy as np
    myList = list(np.around(np.array(myList),2))
    

提交回复
热议问题