Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding
#1
I have a series of data like x = [ 103, 104, 110 , 108, 115] and another number in z = [100]. I get the percentage of changes like changes = (x-z)/z. Now how can I get the the dividend, that means x, of maximum changes? How the code needs to be written?
Reply
#2
Can you show what did you try?
Reply
#3
In your example, you would seek the maximum of the values as that would be the farthest from z (100).
But, if you wanted to actually calculate the deviations
x = [ 103, 104, 110 , 108, 115]
maxdeviant = 0
value_for_maxdeviant = 0
for value in x :
    deviation = abs((value - 100))/100
    if deviation > maxdeviant :
        maxdeviant = deviation
        value_for_maxdeviant = value
print(f"Max deviation is {maxdeviant}, for a value of {value_for_maxdeviant}")        
In the future, we like to see what you have tried and will help you fix your code. I did the above to try to give you a seed to work from. I would expect next questions of
How is that print statement constructed
What is the for loop doing
etc.
Reply
#4
Actually I am not getting any plan for that.
But I convert z into list, which is

[z = 100, 100, 100, 100, 100, 100
differ = np.x-np.z
changes = np.differ/np.z]

Now I need to write for loop. But I cannot find a way out.
Reply
#5
From your question, I don't see why you would make z into an array, nor why you would need numpy. Did you look at my code including the for loop? If that is not what you are looking for, could you rephrase the question?
Reply
#6
@jefsummers your code has worked nicely. Thanks for that. Actually previous comment was done when I was typing and you posted already. Thanks for your help. I am trying to learn python. New in this sector. So don't laugh on my question.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020