You could do it with list comprehensions like so:
somelist = [[22, 13, 31, 13],
[17, 14, 24, 22]]
result = [[1 if (x%10==4 or x%10==9) and (x>=14 and x!=19) else 0 for x in sublist] for sublist in somelist]
result
[[0, 0, 0, 0], [0, 1, 1, 0]]
Where x%10
will get the last digit in each number, allowing the direct comparison. By grouping the two conditions, you can more logically lay out what you want to do, though this gets a bit messy for a list comprehension.
A better way (at the potential expense of speed) might be to use map
:
def check_num(num):
value_check = num >= 14 and num != 19
last_num_check = num % 10 == 4 or num % 10 == 9
return int(value_check and last_num_check)
somelist = [[22, 13, 31, 13],
[17, 14, 24, 22]]
result = [[x for x in map(check_num, sublist)] for sublist in somelist]
result
[[0, 0, 0, 0], [0, 1, 1, 0]]
Timing the difference between operations:
List Comprehension
python -m timeit -s 'somelist = [[22, 13, 31, 13], [17, 14, 24, 22]]' '[[1 if (x%10==4 or x%10==9) and (x>=14 and x!=19) else 0 for x in sublist] for sublist in somelist]'
1000000 loops, best of 3: 1.35 usec per loop
Map
python -m timeit -s 'from somefunc import check_num; somelist = [[22, 13, 31, 13], [17, 14, 24, 22]]' '[[x for x in map(check_num, sublist)] for sublist in somelist]'
100000 loops, best of 3: 3.37 usec per loop