i have list of numbers follows:
given = [[0,5,4,9,0], [2,1,4,9,9]]
i have change numbers follows:
0>>0 1>>1 2>>2 3>>3 4>>3 5>>4 9>>5
or l1 l2.
l1 = [0,1,2,3,4,5,9] l2 = [0,1,2,3,3,4,5]
expected answer is:
ans = [[0,4,3,5,0], [2,1,3,5,5]]
how easy way of doing it?
my trial dirty:
given[given==4]=3 , on.
i prefer numpy method.
use dictionary translator:
>>> given = [[0,5,4,9,0], ... [2,1,4,9,9]] >>> translation = {4:3, 5:4, 9:5} >>> [[translation.get(x, x) x in sub] sub in given] [[0, 4, 3, 5, 0], [2, 1, 3, 5, 5]]
you need supply mapping numbers change, because dict.get(key, default)
return given default value if key not found.
Comments
Post a Comment