Rounding to specific numbers in Python 3.6

I'm trying to make a dive table in which there are some numbers that are not in the template that I see, so I need to manually add all the values, but I need to grab the input and round it to the nearest number in the dictionary.

I will need to convert the input back to a string so that the result is correct:

CODE:

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)

So, when the “player” enters the number 15 for the time, I need to round it to 19 (always up, even if it's 13 or something like that.) I don’t see how I can do this with round (), or I may have to execute a function that checks EVERY number.

+4
source share
5 answers

", ", 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)

:

Please enter Depth (Use numbers!)
35
Please Enter time!
13
B

Please enter Depth (Use numbers!)
35
Please Enter time!
19
B

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

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'

. else for , - .

def getpressureGroup(self, depth, time):

    if depth == "35":
        for max_time, group_name in sorted(self.d35.items()):
            if int(max_time) >= time:
                output = group_name
                break
        else:
            output = "No info for that depth"
    else:
        output = "No info for that depth"
    print(output)
+2

bisect . python , bisect

https://docs.python.org/2/library/bisect.html#other-examples

numpy, , , pandas cut

Python: ,

if 1<x<=18:
    ...
elif 18<x<=28:
    ...
elif

, case { "1": "19", "2": "19"... "20": "25"...} ,

http://code.activestate.com/recipes/415100-invert-a-dictionary-where-values-are-lists-one-lin/

def invert(d):
   return dict( (v,k) for k in d for v in d[k] ) 

d35 = {"10": "A",
                    "19": "B",
                    "25": "C",
                    "29": "D",
                    "32": "E",
                    "36": "F",
                   }

dlist = d35.keys().sort()
d1 = {}
low = -1
for n in dlist[1:]:
    up = int(n) + 1

    interval = range(low, up)
    low = up
    d1[ dlist[i] ] = map(str, interval)


 result = invert(d1)
+1

cut pandas.

, , .

, , .

, :

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)

:

[A, A, A, B, B, C, C, D, E, F]
Categories (6, object): [A < B < C < D < E < F]

, -1, 0 np.inf, .

- dict .

+1

bisect pandas.cut ( ) , Python ( ) . ( @Harvey answer, , .)

d35 = {
    10: "A",
    19: "B",
    25: "C",
    29: "D",
    32: "E",
    36: "F",
}

def pressure_group(time):
    for cutoff in sorted(d35.items()):
        if cutoff[0] >= time:
            return cutoff[1]
    return None  # or pick something else for "too high!"

, , , , , , (, , , ).

Each time through the loop cutoffis a pair of vocabulary d35. Since the pairs are sorted from lowest to highest, you can simply stop at the first one, which is greater than or equal to the input. If the loop ends (because the input is above the maximum cutoff), I decided to return None, but you could return some other value or throw an exception.

+1
source

Source: https://habr.com/ru/post/1674775/


All Articles