Reputation: 13969
I have 2 lists , one containing indexes of max values and the other indexes of min values.
Maxvalues:
c = [209, 255, 285, 327, 358, 380, 396, 419, 447, 476]
Minvalues:
d = [206, 252,254 ,272, 315, 349, 374, 394, 406,413, 440, 466]
I would like to modify the d vector and keep the first min value if there are multiple min values before the corresponding max value in c. The lists contains index values derived from time series. the Max values in list c are correct and should not be modified. However the d list contains multiple mins to the corresponding max index. The rule should be to keep only the first min index in d if there are multiple.. So first min/max index pair is 206/209, second 252/255..so drop 254 in this case..third 272/285 and so on "In the simplified example I would like to delete 254 and 413 in the d list" Min/Max pair should be:
c = [209, 255, 285, 327, 358, 380, 396, 419, 447, 476]
d_new = [206, 252, 272, 315, 349, 374, 394, 406, 440, 466]
I have made a suggestion, however this example removes the first min in d if there are multiple min before the corresponding max value in c.
c = [209, 255, 285, 327, 358, 380, 396, 419, 447, 476]
d = [206, 252,254 ,272, 315, 349, 374, 394, 406, 413, 440, 466]
#beginners trick for reversed looping of max min
c_rev = c[::-1]
d_rev = d[::-1]
output2 = []
for idx, j in enumerate(c_rev):
for jdx, k in enumerate(d_rev):
if d_rev[jdx] <= c_rev[idx]:
output2.append(d_rev[jdx])
break
d_new = output2[::-1]
print(d_new)
print(d_new)
[206, 254, 272, 315, 349, 374, 394, 413, 440, 466]
Appreciate suggestions...
Upvotes: 1
Views: 59
Reputation: 175
Here is a function that should do the job, and should handle the edge cases:
def filter_min_values(max_values, min_values):
"""The input lists must be sorted
>>> filter_min_values([], [])
[]
>>> filter_min_values([10], [1])
[1]
>>> filter_min_values([10], [1])
[1]
>>> filter_min_values([10], [11])
[]
>>> filter_min_values([10], [1, 11])
[1]
>>> filter_min_values([10, 11, 12], [1])
[1]
>>> c = [209, 255, 285, 327, 358, 380, 396, 419, 447, 476]
>>> d = [206, 252, 254, 272, 315, 349, 374, 394, 406, 413, 440, 466]
>>> filter_min_values(c, d)
[206, 252, 272, 315, 349, 374, 394, 406, 440, 466]
"""
new_min_values = []
min_index = 0
for max_index, max_value in enumerate(max_values):
if min_index >= len(min_values):
break
min_value = min_values[min_index]
if min_value <= max_value:
new_min_values.append(min_value)
while min_index < len(min_values) and min_values[min_index] <= max_value:
min_index += 1
return new_min_values
Upvotes: 1
Reputation: 1285
I added a flag
variable, which holds the current maximum value, and will skip values smaller than it in d
c = [209, 255, 285, 327, 358, 380, 396, 419, 447, 476]
d = [206, 252,254 ,272, 315, 349, 374, 394, 406, 413, 440, 466]
#beginners trick for reversed looping of max min
c_rev = c
d_rev = d
output2 = []
flag = 0
for idx, j in enumerate(c_rev):
for jdx, k in enumerate(d_rev):
if k < flag:
continue
if d_rev[jdx] <= c_rev[idx]:
output2.append(d_rev[jdx])
flag = j
break
d_new = output2
print(d_new)
print(d_new)
# [206, 252, 272, 315, 349, 374, 394, 406, 440, 466]
Upvotes: 1
Reputation: 2882
The following could help you into the right direction, but it might not be entirely correct yet.
c = [209, 255, 285, 327, 358, 380, 396, 419, 447, 476]
d = [206, 252, 254 ,272, 315, 349, 374, 394, 406, 413, 440, 466]
d_new2 = []
i = 0
for e in c:
if d[i] <= e:
d_new2.append(d[i])
while i < len(d) and d[i] <= e:
i = i + 1
print(d)
print(d_new2)
So the idea is that the first element of d
that is smaller than the corresponding element in e
qualifies. Then all consecutive values less than e
are skipped.
I'm not convinced that this is a correct solution, but I'll leave it to the OP to judge that. Something like
c = [ 1, 2, 3, 4, 5, 6 ]
d = [100, 101, 102, 103, 104, 105, 106 ]
yields and empty array as output.
At least with the original input, the above yields:
ronald@oncilla:~/tmp$ python x.py
[206, 252, 254, 272, 315, 349, 374, 394, 406, 413, 440, 466]
[206, 252, 272, 315, 349, 374, 394, 406, 440, 466]
Upvotes: 1