Python 3.6中的四舍五入特定数字

2024-09-30 12:16:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个潜水表,其中有些数字不是我能看到的模式,所以我必须手动添加所有值,但我需要获取输入并将其舍入到字典中最近的数字。在

我需要将输入转换回字符串以使输出正确:

代码:

class DepthTable:

    def __init__(self):
        self.d35 = {"10": "A",
                    "19": "B",
                    "25": "C",
                    "29": "D",
                    "32": "E",
                    "36": "F",
                   }



    def getpressureGroup(self, depth, time):

        if depth == "35":
            output = self.d35[time]
        else:
            output = "No info for that depth"
        print(output)


if __name__ == "__main__":
    depthtable = DepthTable()
    print("Please enter Depth (Use numbers!)")
    depth = input()
    print("Please Enter time!")
    time = input()
    depthtable.getpressureGroup(depth,time)

所以当“player”输入数字15作为时间时,我需要将它取整到19(即使是13或类似的数字,也总是向上取整。)我看不出如何使用round()来实现这一点,或者我可能需要创建一个检查每个数字的函数。。在


Tags: selfinputoutputiftimedef数字print
3条回答

d35字典转换为已排序的列表并逐步执行:

In [4]: d35 = {"10": "A",
   ...:                     "19": "B",
   ...:                     "25": "C",
   ...:                     "29": "D",
   ...:                     "32": "E",
   ...:                     "36": "F",
   ...:                    }

In [5]: sorted(d35.items())
Out[5]: [('10', 'A'), ('19', 'B'), ('25', 'C'), ('29', 'D'), ('32', 'E'), ('36', 'F')]

In [7]: time = 15

In [11]: for max_time, group_name in sorted(d35.items()):
    ...:     if int(max_time) >= time:
    ...:         break
    ...:

In [12]: max_time
Out[12]: '19'

In [13]: group_name
Out[13]: 'B'

修改你的方法会得到这个结果。我在for循环中添加了else来处理任何组都没有覆盖的时间。在

^{pr2}$

您可以尝试使用pandas模块中的cut。在

或多或少,它被用来将连续变量分成离散的类别,比如将深度分成压力组。在

您需要指定一个要将数据剪切到其中的容器数组,然后对其进行标记。在

例如:

import pandas as pd
import numpy as np

timestocut = [0, 4, 8, 12, 16, 20, 24, 28, 32, 36]
pd.cut(timestocut, bins = np.array([-1,10,19,25,29,32, np.inf]), labels = np.array(['A','B','C','D','E','F']), right = True)

给予:

^{pr2}$

您可以看到bin有-1,因此我们包含0和np.inf来捕获任何无限大的内容。在

把它集成到你的代码中取决于你——我个人会删除dict并使用这个映射。在

使用“检查每个数字的函数”的思想,可以使用实例变量keys来获取密钥(如果存在),或者获得下一个最高的密钥:

class DepthTable:

    def __init__(self):
        self.d35 = {10: "A",
                    19: "B",
                    25: "C",
                    29: "D",
                    32: "E",
                    36: "F",
                   }

        self.keys = self.d35.keys()


    def getpressureGroup(self, depth, time):
        if depth == 35:
            rtime = min([x for x in self.keys if x >= time]) # if exists get key, else get next largest
            output = self.d35[rtime]
        else:
            output = "No info for that depth"
        print(output)


if __name__ == "__main__":
    depthtable = DepthTable()
    print("Please enter Depth (Use numbers!)")
    depth = int(input())
    print("Please Enter time!")
    time = int(input())
    depthtable.getpressureGroup(depth,time)

演示:

^{pr2}$
Please enter Depth (Use numbers!)
35
Please Enter time!
19
B

Please enter Depth (Use numbers!)
35
Please Enter time!
10
A

相关问题 更多 >

    热门问题