The current code returns erroneous colors.
To make it clear I took the current implementation and put it into a separate function
def new_implementation(value):
if value <= index[0]:
return 0
if value >= index[-1]:
return len(index) - 1
return next(i for i, v in enumerate(color_map.index) if v > value) -1
def current_implementation(value):
if value <= index[0]:
return 0
if value >= index[-1]:
return len(index) - 1
return len([u for u in index if u < value]) - 1
index = [1, 2, 4]
colors = ["black", 'purple', 'red']
print("value", "current implementation", "new implementation")
for i in range(-2, 5):
print(i, "\t", current_implementation(i), "\t", new_implementation(i))
The current code returns erroneous colors. To make it clear I took the current implementation and put it into a separate function
returns:
you will notice that for value
2
the current implementation would return color0
, even though value2
is at index1
inindex
.Or for
this returns
i.e. for index
4
we get color0
and for index5
color2
. Color1
is never returned though it should be returned vor value4
Last but not least I added a check on init to make sure the index is in order