我試圖擴展一個我寫的函數來查找「足夠接近」字典的前3個值也都低於閾值(這裏N = 70)。 :確定高頻區間
d = {
1: {0: 222, 2:44, 18: 44, 20: 22, 21:72, 105:22, 107:9, 115: 66},
2: {0: 61.0, 993: 65.0, 1133: 84.0, 1069: 48.0, 105:22, 107:9, 115: 24, 214:22, 206:9, 225: 241,412: 83.0, 364: 68.0, 682: 64.0, 172: 58.0}
}#nested dictionary
def ff(d):
G = []
for k,v in sorted(d.iteritems()):
G.append((k,v))
#print G
for i in range(len(G)-2):
if (G[i+2][0] - G[i][0] < 20) & (G[i][1] <= 70) & (G[i+1][1] <=70) & (G[i+2][1]<=70):
return i, G[i], G[i+1], G[i+2]
for idnum, ds in sorted(d.iteritems()):
print ff(ds)
輸出:
[(0, 222), (2, 44), (18, 44), (20, 22), (21, 72), (105, 22), (107, 9), (115, 66)]
(1, (2, 44), (18, 44), (20, 22))
[(0, 61.0), (105, 22), (107, 9), (115, 24), (172, 58.0), (206, 9), (214, 22), (225, 241), (364, 68.0), (412, 83.0), (682, 64.0), (993, 65.0), (1069, 48.0), (1133, 84.0)]
(1, (105, 22), (107, 9), (115, 24)) #first interval fitting criteria
我想怎麼辦,實際上是找到長度爲20的所有窗口,並跟蹤它有多少價值有< = 70。任何關於如何開始的想法都會很棒。我似乎不能用「我」要弄清楚如何從狀態移動:
if (G[i+2][0] - G[i][0] < 20) & (G[i][1] <= 70) & (G[i+1][1] <=70) & (G[i+2][1]<=70):
的東西基於長度20,而不是索引?
最終,而不是「前三個」我想保持所有較高頻率的軌道其最小值爲「按順序值< = 70至少3個,連續*和長度爲20間隔」。
期望輸出繼電器:
如果我們有
d[3] = {0: 61.0, 993: 65.0, 1133: 84.0, 1069: 48.0, 105:22, 107:9, 115: 24, 117:22, 200:100, 225: 241,412: 83.0, 420: 68.0, 423: 64.0, 430: 58.0}
會導致輸出:
[(105, 22), (107, 9), (115, 24),(117,22)], [(420, 68.0),(423,63),(430,58)]
# These can be of any length as long as the overall interval of the list is <=20.
可以跳過一個大於70的值嗎?所以,如果不是'(0,222)'你有'(9,222)',那麼'(2,44),(18,44),(20,22)'仍然是正確的嗎? – 2012-03-10 17:36:57
好問題。不,我需要他們**連續**。 – 2012-03-10 19:00:32
如果有重疊的候選窗口怎麼辦? (0,55),(1,55),(2,55),(3,55):前三位,後三位和四位一起似乎都滿足條件。在這種情況下應該發生什麼? – DSM 2012-03-10 19:09:30