May-29-2019, 02:13 PM
You could make use of "max_but_less_than"
max_lt
function to make the code shorterfrom bisect import bisect_left def max_lt(seq, val): # http://code.activestate.com/recipes/415233-getting-minmax-in-a-sequence-greaterless-than-some/#c8 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]]