Jun-05-2019, 07:49 AM
@perfringo,
Well pointed out the ambiguity. Eventhough chances that will occur is small, why not take the effort to handle it the way I would want it to.
So the possibilties:
1. Negative values in element lists.
Right now they aren't included in the resultlist.
It should return the first item in the comparelist. So -1000 (element list) should return first in compare list (nearest)
2. Comparelist has values larger than the largest value in elementlist.
It should return the smallest (so nearest) in the comparelist So in your suggestion that would be 4500.
This is what I think:
Well pointed out the ambiguity. Eventhough chances that will occur is small, why not take the effort to handle it the way I would want it to.
So the possibilties:
1. Negative values in element lists.
Right now they aren't included in the resultlist.
It should return the first item in the comparelist. So -1000 (element list) should return first in compare list (nearest)
2. Comparelist has values larger than the largest value in elementlist.
It should return the smallest (so nearest) in the comparelist So in your suggestion that would be 4500.
This is what I think:
#comparelist = [4500, 5000, 6000] comparelist = [0, 3000, 6000] elements = [[-1,3], [4, 400], [3,4222]] result = [] for element in elements: result.append([]) for num in element: if all(value > num for value in comparelist): result[-1].append(comparelist[0]) else: for value in reversed(comparelist): if num > value: result[-1].append(value) break print(result)