python - How to get numpy array from multiple lists of same length and sort along an axis? -
i have simple question ,how numpy array multiple lists of same length , sort along axis ?
i'm looking like:
a = [1,1,2,3,4,5,6] b = [10,10,11,09,22,20,20] c = [100,100,111,090,220,200,200] d = np.asarray(a,b,c) print d >>>[[1,10,100],[1,10,100],[2,11,111].........[6,20,200]]
2nd question : , if achieved can sort along axis (for eg. on values of list b)?
3rd question : can sorting done on range ? eg. values between b+10 , b-10 while looking @ list c further sorting. like
[[1,11,111][1,10,122][1,09,126][1,11,154][1,11,191] [1,20,110][1,25,122][1,21,154][1,21,155][1,21,184]]
you can zip array:
a = [1, 1, 2, 3, 4, 5, 6] b = [10, 10, 11, 9, 22, 20, 20] c = [100, 100, 111, 90, 220, 200, 200] d = np.asarray(zip(a,b,c)) print(d) [[ 1 10 100] [ 1 10 100] [ 2 11 111] [ 3 9 90] [ 4 22 220] [ 5 20 200] [ 6 20 200]] print(d[np.argsort(d[:, 1])]) # sorted copy [[ 3 9 90] [ 1 10 100] [ 1 10 100] [ 2 11 111] [ 5 20 200] [ 6 20 200] [ 4 22 220]]
i don't know how inplace sort without doing like:
d = np.asarray(zip(a,b,c)) d.dtype = [("0", int), ("1", int), ("2", int)] d.shape = d.size d.sort(order="1")
the leading 0
make 090
octal in python2 or invalid syntax in python3 removed it.
you can sort zipped elements before pass the:
from operator import itemgetter zipped = sorted(zip(a,b,c),key=itemgetter(1)) d = np.asarray(zipped) print(d) [[ 3 9 90] [ 1 10 100] [ 1 10 100] [ 2 11 111] [ 5 20 200] [ 6 20 200] [ 4 22 220]]
Comments
Post a Comment