Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Class method returning multiple values,dont know which is returned when..
#1
Dear all, Good morning from Germany,
I have a python code in which the function returns two values. Out of which, only one value changes at a time and other remains the same. I want to find which is changing and want to append it. The code looks as following
def calcrand(self,xlowrand,xhighrand):
        xalg = self.xalg
        yalg = self.yalg
        xlowrand = self.xlowrand
        xhighrand = self.xhighrand
                        
        if (len(xalg))>=2 and len(yalg)>=2:
            last = len(yalg)-1
            xPrevious = xalg[last-1]
            xLast = xalg[last]    
            yPrevious = yalg[last-1]
            yLast = yalg[last]
            prevdist= abs(yalg[last-1]-ygoal)
            currentdist= abs(yalg[last]-ygoal)
                                                          
            if currentdist<prevdist:
                m=float ((yLast-yPrevious)/(xLast-xPrevious)) 
                
                if m>0:
                    if yalg[last]< ygoal:
                        xlowrand=xalg[last]
                        return float(xlowrand),float(xhighrand),
                    else:
                        xhighrand=xalg[last]
                        return float(xlowrand),float(xhighrand)
                elif yalg[last]< ygoal:
                    xhighrand=xalg[last]
                    return float(xlowrand),float(xhighrand)
                else:
                    xlowrand=xalg[last]
                    return float(xlowrand),float(xhighrand)              
                                         
        elif (len(xalg))==1:
            return float(xlowrand),float(xhighrand)
    
        elif (len(xalg))<1:
            return float(xlowrand),float(xhighrand)

        else:
            print('there are no data')
            
        return float(xlowrand),float(xhighrand)
I run this in a loop...
Quote
#2
Hello,

Since you are already returning a tuple, just add a third value which indicates what the other two values are.
this can be a code, 1 - condition 1, 2 - condition 2, ... or it can be a string like ,m < 0'

Larz60+
Quote
#3
Hello, I am still not clear what do you mean??

I get output as following when I run it in loop

Quote:returnvalue 0.0 9.0
returnvalue 0.0 7.59979666373
returnvalue 0.0 7.59979666373
returnvalue 0.0 7.59979666373
returnvalue 0.0 7.59979666373
returnvalue 3.8855839217 7.59979666373
returnvalue 5.3895954547 7.59979666373
returnvalue 7.12194804382 7.59979666373
returnvalue 7.26688561137 7.59979666373
returnvalue 7.26688561137 7.42555000493
returnvalue 7.26688561137 7.42555000493
returnvalue 7.41097079158 7.42555000493

for the first iteration (0 , 9) is the return than (0 , 7.599) so I only want to append like (0, 9, 7.599, 3.88,  5.38, )
Quote
#4
Hello,

Taking the following snippet of your code:

if currentdist<prevdist:
               m=float ((yLast-yPrevious)/(xLast-xPrevious))
               
               if m>0:
                   if yalg[last]< ygoal:
                       xlowrand=xalg[last]
                       return float(xlowrand),float(xhighrand),
                   else:
                       xhighrand=xalg[last]
                       return float(xlowrand),float(xhighrand)
you could add a list which shows how the values were constructed like:

msg = []
if currentdist < prevdist:
    msg.append('currentdist < prevdist')
    m = float((yLast - yPrevious) / (xLast - xPrevious))

    if m > 0:
        msg.append('m > 0')
        if yalg[last] < ygoal:
            xlowrand = xalg[last]
            return float(xlowrand), float(xhighrand), msg
        else:
            msg.append('m <= 0')
            xhighrand = xalg[last]
            return float(xlowrand), float(xhighrand), msg
Now results will include a list like:

3.8855839217, 7.59979666373,  ['currentdist < prevdist', 'm <= 0']
Larz60+
Quote
#5
Thank you it has helped me. Nevertheless I have 1 new issue which I have posted maybe you can have a look.!!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to pass multiple values from one sample to nc variable? Baloch 0 96 Jun-01-2020, 09:27 PM
Last Post: Baloch
  Inserting values from multiple lists sqlite azulu 1 257 May-24-2020, 08:40 AM
Last Post: ibreeden
  calling on a method from one class into another class which is not a child NABA 5 171 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  UnUnloading values from multiple widgets in a container UGuntupalli 3 249 Apr-20-2020, 08:53 PM
Last Post: UGuntupalli
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 637 Feb-12-2020, 04:11 PM
Last Post: psolar
  How to use a returned value? t4keheart 12 516 Jan-16-2020, 06:54 AM
Last Post: perfringo
  threading for method outside class anna 0 171 Nov-17-2019, 07:05 AM
Last Post: anna
  how can I changing a base class method voidptr 2 221 Nov-10-2019, 10:53 PM
Last Post: voidptr
  How to Call a method of class having no argument dataplumber 7 740 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Save all values to pandas of multiple classes jenniferruurs 0 260 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs

Forum Jump:


Users browsing this thread: 1 Guest(s)