May-29-2019, 02:13 PM
You could make use of "max_but_less_than"
max_lt
function to make the code shorter1 2 3 4 5 6 7 8 9 10 11 12 |
from bisect import bisect_left def max_lt(seq, val): return seq[bisect_left(seq, val) - 1 ] thres = [ 0 , 3000 , 6000 ] nested_list = [[ 3 ], [ 4 , 400 ], [ 3 , 4222 ]] substituted = [[max_lt(thres, val) for val in seq] for seq in nested_list] print (substituted) |
Output:[[0], [0, 0], [0, 3000]]