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.
Here’s something which might help you get started. It’s loop-based, and doesn’t even use zip (much less itertools.takewhile!), but hopefully will make sense:
I used “break” because as soon as we have any value > max_value or we’re >= upper_length there are no more possible windows starting at start_index.
If you haven’t seen
yieldbefore, it makes the function into a generator function; it’s like areturnwhere the function sends back (yields) the value and then can continue instead of stopping. (See the answers to this question for more information.)It’s still not entirely clear to me what you want to do with overlapping windows, but currently it finds them all and you can decide either within the function or in post-processing how you want to handle that.
Modifying the code to not test for whether all the values are <= max_value and count them instead should be trivial, so I’ll leave that alone.