Change default float print format

后端 未结 7 551
无人共我
无人共我 2020-12-01 16:44

I\'ve some lists and more complex structures containing floats. When printing them, I see the floats with a lot of decimal digits, but when printing, I don\'t need all of th

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 17:12

    This doesn't answer the more general question of floats nested in other structures, but if you just need to print floats in lists or even array-like nested lists, consider using numpy.

    e.g.,

    import numpy as np
    np.set_printoptions(precision=3, suppress=False)
    list_ = [[1.5398, 2.456, 3.0], 
             [-8.397, 2.69, -2.0]]
    print(np.array(list_))
    

    gives

    [[ 1.54   2.456  3.   ]
     [-8.397  2.69  -2.   ]]
    

提交回复
热议问题