EmJ
EmJ

Reputation: 4608

How to find the longest consecutive subsequence of a list greater than mean in python

I want to find the length of the longest consecutive subsequence of a list that has the value greater than its mean.

For instance, consider the below example.

mylist = [0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0]

The mean of the above list is 9.181818181818182. So, the longest consecutive subsequence is [25,20,15]. Hence, the length is 3.

I tried to do it as follows.

mytemp = []
for item in mylist:
    if item > np.mean(mylist).item():
        mytemp.append(1)
    else:
        mytemp.append(0)
print(mytemp)

However, this is inefficinet for long datasets as I am creating another array to do this. I am wondering if there is a more efficient way to do this in python.

I am happy to provide more details if needed.

Upvotes: 1

Views: 972

Answers (3)

Lee
Lee

Reputation: 1427

With only standard librarys:

from itertools import groupby
from statistics import mean
mylist=[0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0]
m=mean(mylist)
mylist=[tuple(x) for b,x in groupby(mylist,key=lambda x:x>m) if b]
print(max(mylist,key=len))

It serch runns of above and below or equal. Than keeps the above get it length and print only the one with max length.

Upvotes: 1

Divakar
Divakar

Reputation: 221514

With NumPy arrays and funcs for efficiency -

a = np.array(mylist)
m = np.r_[False,a>a.mean(),False]
idx = np.flatnonzero(m[:-1]!=m[1:])
largest_island_len = (idx[1::2]-idx[::2]).max()

If you need the elements too -

I = (idx[1::2]-idx[::2]).argmax()
elems = a[idx[2*I]:idx[2*I+1]]

Upvotes: 1

Teymour
Teymour

Reputation: 2079

Could you use the filter function?

e.g.

mean = sum(mylist)/len(mylist)
mytemp = filter(lambda x: x > mean, mylist)

To increase the speed of your program you might want to look into using a C/C++/Fortran library (e.g. numpy), perhaps one which provides GPU acceleration (e.g. tensorflow, pytorch).

Upvotes: 1

Related Questions