Home > Back-end >  python efficient if statements conditions
python efficient if statements conditions

Time:10-23

I created a sample program for 3 data points however, I have many data points and need more efficient code to run. The logic is I am comparing every Pi with its next P(i 1) and post comparing all the differences, I am selecting the max value and taking its relevant BSPi & SSPi. additional condition is if P[i] is greater than p[i 1]; it should be greater than 50.

sp1=100
sp2=150
sp3=200
sp4=250

p1=90
p2=40
p3=120
p4=150


if p1-p2>=0:
    d1=p1-p2-50
    bsp1=sp2
    ssp1=sp1
else:
    d1=p2-p1
    bsp1=sp1
    ssp1=sp2



if p2-p3>=0:
    d2=p2-p3-50
    bsp2=sp3
    ssp2=sp2
else:
    d2=p3-p2
    bsp2=sp2
    ssp2=sp3



if p3-p4>=0:
    d3=p3-p4-50
    bsp3=sp4
    ssp3=sp3
else:
    d3=p4-p3
    bsp3=sp3
    ssp3=sp3

data = {'d1': d1,'d2': d2, 'd3': d3,}
max_data=max(data, key=data.get)
if max_data=='d1':
    bsp=bsp1
    ssp=ssp1
elif max_data=='d2':
    bsp=bsp2
    ssp=ssp2
else:
    bsp=bsp3
    ssp=ssp3

print(bsp)
print(ssp)

CodePudding user response:

Create an array to contain sp = [sp1, ... , spn] Create an array to contain p = [p1, ... , pn] Initialize a value max = 0 Initialize a value index = 0

Iterate through range of p (given that len(p) > 1), calculate the difference of i and i 1 elements. If the difference is greater than max, store the difference and store the index.

In the end, store into bsp = sp[index] and ssp = sp[index 1]

def find_max_in_data(p, sp):
    max_diff = 0
    index = 0

    for i in range(len(p)-1):
        diff = abs(p[i 1] - p[i])
        if diff > max_diff:
            max_diff = diff
            index = i
    
    ssp = sp[index]
    bsp = sp[index 1]

EDIT: For more particular code,

def find_max_in_data(p, sp):
    max_diff = 0
    bspindex = 0
    sspindex = 0

    for i in range(len(p)-1):
        if p[i] - p[i 1] >= 0:
            diff = p[i] - p[i 1] - 50
            if diff > max_diff:
                max_diff = diff
                bspindex = i 1
                sspindex = i
        else:
            diff = p[i 1] - p[i]
            if diff > max_diff:
                max_diff = diff
                bspindex = i
                sspindex = i 1
    
    ssp = sp[sspindex]
    bsp = sp[bspindex]

CodePudding user response:

I suggest a different approach.

Rather than defining many discrete variables, write a class that incorporates all of the associated variables for a "data point". Something like this (with full property, str and repr functionality):

class DataPoint:
    def __init__(self, p, sp):
        self._p = p
        self._sp = sp
        self._ssp = None
        self._bsp = None
        self._d = None
    @property
    def p(self):
        return self._p
    @property
    def sp(self):
        return self._sp
    @property
    def ssp(self):
        return self._ssp
    @ssp.setter
    def ssp(self, v):
        self._ssp = v
    @property
    def bsp(self):
        return self._bsp
    @bsp.setter
    def bsp(self, v):
        self._bsp = v
    @property
    def d(self):
        return self._d
    @d.setter
    def d(self, v):
        self._d = v
    def __repr__(self):
        return f'{self.p=} {self.sp=} {self.ssp=} {self.bsp=} {self.d=}'
    def __str__(self):
        return f'p={self.p}, sp={self.sp}, ssp={self.ssp}, bsp={self.bsp}, d={self.d}'

Using this structure you can build a list of classes like this:

data_points = []

data_points.append(DataPoint(90, 100))
data_points.append(DataPoint(40, 150))
data_points.append(DataPoint(120, 200))
data_points.append(DataPoint(150, 250))

Note how the p and sp values relate to the ones in the question.

Now that we have a list of these classes we can write a single piece of code to process them and populate them with appropriate values.

for dpa, dpb in zip(data_points, data_points[1:]):
    if dpa.p - dpb.p >= 0:
        dpa.d = dpa.p - dpb.p - 50
        dpa.bsp = dpb.sp
        dpa.ssp = dpa.sp
    else:
        dpa.d = dpb.p - dpa.p
        dpa.bsp = dpa.sp
        dpa.ssp = dpb.sp
    print(dpa)

This gives the following output:

p=90, sp=100, ssp=100, bsp=150, d=0
p=40, sp=150, ssp=200, bsp=150, d=80
p=120, sp=200, ssp=250, bsp=200, d=30

Note that there are just 3 outputs. This is as expected due to the way the comparisons are carried out.

  • Related