I constructed the following dictionary, which obtains counters starting from a for-loop
. The for-loop
does something like this:
for period in range(number_periods):
if value > mean_value:
dict[key] += 1
From this, I get the following dictionary:
dict = {(1, 1): 0, (2, 2): 0, (3, 3): 3}
Now I want to figure out that if a key in the dictionary gets a counter in, e.g. three consecutive periods of the for-loop
(i.e. k=3
), I want to identify and store these keys in a new dictionary separately.
Hence, if the following happens, I want to create a new dictionary or print the specific key in which the counters increased consecutively.
period = 1: dict = {(1, 1): 0, (2, 2): 0, (3, 3): 0}
period = 2: dict = {(1, 1): 0, (2, 2): 0, (3, 3): 1}
period = 3: dict = {(1, 1): 0, (2, 2): 0, (3, 3): 2}
period = 4: dict = {(1, 1): 0, (2, 2): 0, (3, 3): 3}
new_dict = {(3, 3): 1}
That is, the new_dict
represents the keys in which this consecutive increment has occurred (which happened exactly once if k=3
for the given example.
The new_dict
itself should feature a counting feature, i.e. whenever the counter in dict
increases three consecutive times, a counter in new_dict
should increase by 1
. If this happens another time, the new_dict
should be adjusted again:
new_dict = {(3, 3): 2}
What is the most efficient way to achieve this?
One way you might want this to happen is to check when you increment a value in dict
:
for period in range(number_periods):
if value > mean_value:
dict[key] += 1
v = dict[key]
v3 = v//3
if v3 > 0:
new_dict[key] = v3