【问题标题】:Printing a tuple in Python with user-defined precision在 Python 中以用户定义的精度打印元组
【发布时间】:2016-08-22 12:57:00
【问题描述】:

Printing tuple with string formatting in Python 之后,我想打印以下元组:

tup = (0.0039024390243902443, 0.3902439024390244, -0.005853658536585366, -0.5853658536585366)

只有 5 位精度。我怎样才能做到这一点?

(我试过print("%.5f" % (tup,)),但我得到了TypeError: not all arguments converted during string formatting)。

【问题讨论】:

  • 元组没有精度,浮点数有。而且您正在尝试将元组转换为浮点数,这没有任何意义。

标签: python


【解决方案1】:

您可以“像元组一样”以自定义精度打印浮点数:

>>> tup = (0.0039024390243902443, 0.3902439024390244, -0.005853658536585366, -0.5853658536585366)
>>> print('(' + ', '.join(('%.5f' % f) for f in tup) + ')')
(0.00390, 0.39024, -0.00585, -0.58537)

【讨论】:

    【解决方案2】:

    可能的解决方法:

    tup = (0.0039024390243902443, 0.3902439024390244, -
           0.005853658536585366, -0.5853658536585366)
    
    print [float("{0:.5f}".format(v)) for v in tup]
    

    【讨论】:

      【解决方案3】:

      尝试以下(列表理解)

      ['%.5f'% t for t in tup]
      

      【讨论】:

        【解决方案4】:

        试试这个:

        class showlikethis(float):
            def __repr__(self):
                return "%0.5f" % self
        
        tup = (0.0039024390243902443, 0.3902439024390244, -0.005853658536585366, -0.5853658536585366)
        tup = map(showlikethis, tup)
        print tup
        

        您可能想重新引用您的问题,元组 dnt 有精度。

        【讨论】:

        • 哦,投反对票的人,强大的聪明人:一个没有解释的小评论会帮助我们。
        【解决方案5】:

        您可以处理单个项目。 试试这个:

        >>> tup = (0.0039024390243902443, 0.3902439024390244, -0.005853658536585366, -0.5853658536585366)
        >>> for t in tup:
            print ("%.5f" %(t))
        
        
        0.00390
        0.39024
        -0.00585
        -0.58537
        

        【讨论】:

          【解决方案6】:

          您可以像这样遍历元组,然后打印结果 对于 python > 3

          ["{:.5f}".format(i) for i in tup] 
          

          对于 python 2.7

          ['%.5f'% t for t in tup]
          

          【讨论】:

            【解决方案7】:

            实现此目的的大多数 Pythonic 方法是使用 map()lambda() 函数。

            >>> map(lambda x: "%.5f" % x, tup)
            ['0.00390', '0.39024', '-0.00585', '-0.58537']
            

            【讨论】:

              【解决方案8】:

              我想出了另一个使用 Numpy 的解决方法:

              import numpy as np
              np.set_printoptions(precision=5)
              print(np.array(tup))
              

              产生以下输出:

              [ 0.0039   0.39024 -0.00585 -0.58537]
              

              【讨论】:

                【解决方案9】:

                这里有一个方便的函数供python>3.6为你处理一切:

                def tuple_float_to_str(t, precision=4, sep=', '):
                    return '({})'.format(sep.join(f'{x:.{precision}f}' for x in t))
                

                用法:

                >>> print(funcs.tuple_float_to_str((12.3456789, 8), precision=4))
                (12.3457, 8.0000)
                

                【讨论】:

                  猜你喜欢
                  • 2016-04-30
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-09-30
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-02-02
                  相关资源
                  最近更新 更多