我正在尝试创建一个潜水表,其中有些数字不是我能看到的模式,所以我必须手动添加所有值,但我需要获取输入并将其舍入到字典中最近的数字。在
我需要将输入转换回字符串以使输出正确:
代码:
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()来实现这一点,或者我可能需要创建一个检查每个数字的函数。。在
将
d35
字典转换为已排序的列表并逐步执行:修改你的方法会得到这个结果。我在for循环中添加了else来处理任何组都没有覆盖的时间。在
^{pr2}$您可以尝试使用
pandas
模块中的cut
。在或多或少,它被用来将连续变量分成离散的类别,比如将深度分成压力组。在
您需要指定一个要将数据剪切到其中的容器数组,然后对其进行标记。在
例如:
给予:
^{pr2}$您可以看到bin有-1,因此我们包含0和
np.inf
来捕获任何无限大的内容。在把它集成到你的代码中取决于你——我个人会删除dict并使用这个映射。在
使用“检查每个数字的函数”的思想,可以使用实例变量
keys
来获取密钥(如果存在),或者获得下一个最高的密钥:演示:
^{pr2}$相关问题 更多 >
编程相关推荐