I'm trying to extend a function I wrote to find the first 3 values of a dictionary that are "close enough" are also all below a threshold (here 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)
Output:
[(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
What I'd like to do, is actually find ALL windows of length 20 and keep track of how many values it has <=70. Any thoughts on how to get started would be great. I can't seem to figure out how to move from the condition using "i":
if (G[i+2][0] - G[i][0] < 20) & (G[i][1] <= 70) & (G[i+1][1] <=70) & (G[i+2][1]<=70):
to something based on the length 20 and not the indexing??
Ultimately, instead of "the first three" i'd like to keep track of all higher frequency with the minimum being "at least 3 of value <=70, consecutive in order* and in length 20 interval".
Desired Ouput:
If we have
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}
Would result in the output:
[(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.