SoFunction
Updated on 2024-12-13

Methods for interval fetching using python

Requirements Background:

Carry out the score calculation. Below, if just one or two is okay, write the judgment, but if there are dozens, hundreds, will it be miserable. Moreover, the following is still three cases.

Example:

Solution:

# compare lists by value, value lists, return interval values, other_value i.e. not in the case of
    def get_value_by_between(self, compare_value, compare_list, value_list, other_value, type="compare", left=False,
                             right=True):
        try:
            if compare_value is None or compare_value == '':
                return other_value
 
            if len(compare_list) != len(value_list):
                raise Exception("Interval comparison of two lists of inconsistent length")
 
            # # If the value compared matches the value of the other case, it is the other case
            # if compare_value == other_value:
            #     return other_value
 
            # Left open interval
            if compare_list[0] == -9999999 and compare_list[1] >= compare_value:
                return value_list[0]
 
            # Right open interval
            if right is True and compare_value > compare_list[len(compare_list) - 1]:
                return value_list[len(compare_list) - 1]
            # Left open interval
            # if left is True and compare_value <= compare_list[0]:
            #     return compare_value[0]
 
            for ind, this_val in enumerate(compare_list):
                # Returns the last value if it is the last one
                if compare_value > compare_list[len(compare_list) - 1]:
                    return value_list[len(compare_list) - 1]
                # Returns the default
                elif (ind + 1) == len(compare_list):
                    return other_value
 
                # Next, if greater than the length of compare_list minus 1, then return last
                next_val = compare_list[ind if ind >= len(compare_list) else ind + 1]
                # For the first one, it's greater than or equal to, less than the next one #
                if ind == 0 and compare_value >= this_val and compare_value <= next_val:
                    return value_list[ind]
                # Greater than the left, less than or equal to the right
                elif this_val < compare_value and compare_value <= next_val:
                    return value_list[ind]
        except:
            ("Calculating score anomalies based on intervals.", traceback.format_exc())
        return other_value
# Digital
    def get_val_by_list(self, compare_value, compare_list, val_list, other_value):
        try:
            if compare_value is None:
                return other_value
 
            for ind, li in enumerate(compare_list):
                if len(li) == 1 and compare_value == li[0]:
                    return val_list[ind]
                # The last one
                elif len(li) == 1 and (ind + 1) == len(compare_list) and compare_value >= li[0]:
                    return val_list[ind]
                elif len(li) == 2 and compare_value >= li[0] and compare_value <= li[1]:
                    return val_list[ind]
        except:
            (" get_val_by_list exception ", traceback.format_exc())
        return other_value

TEST

# creditTime i.e. value
self.get_val_by_list(creditTime, [[0],[1],[2],[3]], [20, 10, 0, -100],
                                                                   other_value=0)
self.get_value_by_between(taxCreditRating, [0, 60, 70, 80, 90],[-200, 0, 10, 20, 30], other_value=0)

In the case of Figure 2, the third case, an extra 0, and the corresponding value, would need to be added.

self.get_value_by_between(taxAmt12m, [0,0, 1000, 15000, 50000, 200000],[-50, -50, 0, 0, 5, 10], -0)

If negative infinity, use -999999

to this article on the use of python interval value of the article is introduced to this, more related python interval value of the content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!