ED314
ED314

Reputation: 173

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.

Upvotes: 1

Views: 138

Answers (2)

Patrick Bormann
Patrick Bormann

Reputation: 749

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)

Upvotes: 2

Pygirl
Pygirl

Reputation: 13349

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])

Upvotes: 1

Related Questions