Search code examples
pythonarraysnumpyvariablesmax

Take a variables into array and find maximun value


Using numpy I have this variables:

A01 = 10  
A02 = 8  
A03 = 3  
A04 = 4  
A05 = 12

And this comparisons

C01 = abs(A01-A02)
C02 = abs(A01-A03)
C03 = abs(A01-A04)
C04 = abs(A01-A05)
C05 = abs(A01-A06)

I need take C01, C02, C03, C04, C05 into array and find maximun value, How I to do this? My objetive is find the maximum value of comparisons based on the list of variables


Solution

  • You create an array with those variables, and compare the first index to the rest via slicing. Then you can use np.abs and np.max to evaluate the entire array.

    import numpy as np
    A0, A1, A2, A3, A4 = 10, 8, 3, 4, 12
    compare = np.array([A0, A1, A2, A3, A4])
    max_val = np.max(np.abs(compare[0]-compare[1:]))
    print(max_val) #Should be 7