【发布时间】:2019-08-26 13:59:34
【问题描述】:
我在尝试将索引的最后一个条目包含到输出时遇到了障碍。 一对峰值由列表中大于 3 的一组相邻值定义。 如何将最后一个条目的索引包含在输出中?
data_series_1 = [6,4,5,2,2,0,5,4,4,2,0,2,2,1,4,2,2,5,4,6]
def paired_peaks(data_series,threshold):
peaks =[]
for k in range(0,len(data_series)-1):
y_b = data_series[k-1]
y= data_series[k]
y_a = data_series[k+1]
if y>threshold:
if y_b>threshold or y_a>threshold:
peaks.append(k)
return peaks
print(paired_peaks(data_series_1,3))
我希望它是[0, 1, 2, 6, 7, 8, 17, 18, 19],但实际输出是[0, 1, 2, 6, 7, 8, 17, 18]。
【问题讨论】:
-
您的循环在检查最后一个索引之前退出...
标签: python python-3.x list for-loop indexing