【问题标题】:Rounding a list of floats into integers in Python在 Python 中将浮点数列表舍入为整数
【发布时间】:2020-05-13 21:49:03
【问题描述】:

我有一个数字列表,在继续使用该列表之前,我需要将其四舍五入。示例源列表:

[25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]

如何保存这个所有数字都舍入为整数的列表?

【问题讨论】:

    标签: python list rounding


    【解决方案1】:

    只需对所有具有列表理解的列表成员使用round 函数:

    myList = [round(x) for x in myList]
    
    myList # [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
    

    如果您希望 round 具有一定的精度 n 使用 round(x,n)

    【讨论】:

      【解决方案2】:

      您可以将内置函数 round() 与列表理解一起使用:

      newlist = [round(x) for x in list]
      

      你也可以使用内置函数map()

      newlist = list(map(round, list))
      

      不过,我不建议将 list 用作名称,因为您隐藏了内置类型。

      【讨论】:

      • 您确定您的第二种方法不需要列表转换吗?
      • @muyustan:你是对的。在我写这个答案的时候,我主要使用 Python 2,其中map 已经返回了一个列表,但是是的,这已经改变了。
      【解决方案3】:

      如果你要设置你可以做的有效位数

      new_list = list(map(lambda x: round(x,precision),old_list))
      

      此外,如果你有一个你可以做的列表列表

      new_list = [list(map(lambda x: round(x,precision),old_l)) for old_l in old_list]
      

      【讨论】:

        【解决方案4】:

        你可以使用python内置的round函数。

        l = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
        
        list = [round(x) for x in l]
        
        print(list)
        

        输出是:

        [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
        

        【讨论】:

          【解决方案5】:

          使用map 函数的另一种方法。

          您可以设置round的位数。

          >>> floats = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
          >>> rounded = map(round, floats)
          >>> print rounded
          [25.0, 193.0, 282.0, 88.0, 80.0, 450.0, 306.0, 282.0, 88.0, 676.0, 986.0, 306.0, 282.0]
          

          【讨论】:

            【解决方案6】:

            NumPy 非常适合处理这样的数组。
            只需 np.around(list)np.round(list) 即可。

            【讨论】:

              【解决方案7】:

              为 python3 更新这个,因为其他答案利用 python2 的 map,它返回一个 list,其中 python3 的 map 返回一个迭代器。您可以让 list 函数使用您的 map 对象:

              l = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
              
              list(map(round, l))
              [25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
              

              要以这种方式将round 用于特定的n,您需要使用functools.partial

              from functools import partial
              
              n = 3
              n_round = partial(round, ndigits=3)
              
              n_round(123.4678)
              123.468
              
              new_list = list(map(n_round, list_of_floats))
              
              

              【讨论】:

                猜你喜欢
                • 2022-12-07
                • 1970-01-01
                • 2020-10-01
                • 1970-01-01
                • 2013-06-29
                • 2021-10-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多