Map list to list of lists by index (Python) -
i'm looking efficient way map list of values,
x = [1,2,3,4,5,6,7,8,9]
to list, containing lists of values of same total range, example
z = [[1,2,3], [4,5,6], [7,8,9]].
what need know index value x corresponds in z. x[0]=1
correspond z[0]
, because falls in first bin of z, x[7]=8
correspond z[2]
, because falls in third bin.
how identify index of sublists of z each x value? in case, x has 50,000 values, z has 5,000 sublists, , have lots of lists, i'm looking way fast possible.
you can use dict here:
>>> x = [1,2,3,4,5,6,7,8,9] >>> z = [[1,2,3], [4,5,6], [7,8,9]] >>> dic = {y :i i,a in enumerate(z) y in a} >>> dic[7] 2 >>> dic[8] 2 >>> dic[1] 0
Comments
Post a Comment