简体   繁体   English

如何在python中找到大于平均值的列表的最长连续子序列

[英]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.以上列表的平均值为 9.181818181818182。 So, the longest consecutive subsequence is [25,20,15].因此,最长的连续子序列是 [25,20,15]。 Hence, the length is 3.因此,长度为 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.我想知道在 python 中是否有更有效的方法来做到这一点。

I am happy to provide more details if needed.如果需要,我很乐意提供更多详细信息。

Could you use the filter function?可以使用filter功能吗?

eg例如

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 (eg numpy), perhaps one which provides GPU acceleration (eg tensorflow, pytorch).为了提高程序的速度,您可能需要考虑使用 C/C++/Fortran 库(例如 numpy),也许是提供 GPU 加速的库(例如 tensorflow、pytorch)。

With NumPy arrays and funcs for efficiency -使用 NumPy 数组和函数来提高效率 -

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

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.比保持上面的得到它的长度并只打印最大长度的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM