Skip to content
Advertisement

How to find the maximum number in python repeatedly from a group of numbers

I have a numpy array like below:

[12,544,73,56,30,84,34,29,78,22,73,23,98,83,35,62,52,94,44,67] 

In this data there are 20 numbers and they are divided in 4 groups with 5 numbers in each group. so for ex.

12,544,73,56,30
84,34,29,78,22 etc.

I want to find out the maximum number from each group and store them in a list. Like:

sol=[544,84,98,94]

I am very new to python please help.

Advertisement

Answer

try by splitting 1st then find out the max.

x = np.array([12,544,73,56,30,84,34,29,78,22,73,23,98,83,35,62,52,94,44,67])
n = 4
res = np.array(np.array_split(x, n)).max(axis=1)

res:

array([544,  84,  98,  94])
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement