1

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.

ED314
  • 183
  • 8
  • 1
    split into chunks and compute max for each one: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks – pieca Mar 24 '21 at 09:42

2 Answers2

2

Something like that?

import pandas as pd
field = [12,544,73,56,30,84,34,29,78,22,73,23,98,83,35,62,52,94,44,67]
field = pd.DataFrame(field)
field.rolling(window = 5, win_type = None).max().iloc[4::5]

gives:

4   544.0
9   84.0
14  98.0
19  94.0

Every 5th step

Update and a much faster one:

field = np.array([12,544,73,56,30,84,34,29,78,22,73,23,98,83,35,62,52,94,44,67])
field.reshape(-1, 5).max(axis=1)
Patrick Bormann
  • 729
  • 6
  • 16
1

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])
Pygirl
  • 12,969
  • 5
  • 30
  • 43