Home > Blockchain >  Python 2D Array Sorting is not showing actual result
Python 2D Array Sorting is not showing actual result

Time:10-10

I am trying to sort a 2d array, i tried many ways but its not working .I don't know what's going on here

    from operator import itemgetter
    import pprint
    arr=[['0.55070000', '879.00000000'], ['0.55350000', '879.00000000'], ['0.55640000', '879.00000000'], ['0.56210000', '879.00000000'], ['0.56490000', '879.00000000'], ['0.56780000', '879.00000000'], ['0.57060000', '879.00000000'], ['0.54500000', '899.00000000'], ['0.56370000', '901.00000000'], ['0.53070000', '908.00000000'], ['0.57350000', '916.00000000'], ['0.55000000', '9172.00000000'], ['0.56300000', '939.00000000'], ['0.54490000', '94.00000000'], ['0.56040000', '96.00000000'], ['0.54960000', '999.00000000']]
    # sorted(floatarry, key=itemgetter(1))
    sorted(arr,key=lambda x:x[1])
    pprint.pprint(arr)
    # from operator import itemgetter
    
    #############################################################
    # columnIndex = 1
    # # Sort 2D numpy array by 2nd Column
    
    # arr2D = np.array(arr)
    
    # sortedArr = arr2D[arr2D[:,columnIndex].argsort()]
    # print('Sorted 2D Numpy Array')
    # print(sortedArr)

result 
[['0.55070000' '879.00000000']
['0.55350000' '879.00000000']
['0.55640000' '879.00000000']
['0.56210000' '879.00000000']
['0.56490000' '879.00000000']
['0.56780000' '879.00000000']
['0.57060000' '879.00000000']
['0.54500000' '899.00000000']
['0.56370000' '901.00000000']
['0.53070000' '908.00000000']
['0.57350000' '916.00000000']
['0.55000000' '9172.00000000']
['0.56300000' '939.00000000']
['0.54490000' '94.00000000']
['0.56040000' '96.00000000']
['0.54960000' '999.00000000']]

last 4 and 5 no line showing wrong result

CodePudding user response:

The sorted() function is not changing the arr itself, it simply returns a sorted list that you then have to assign to some variable. So do it like this:

from operator import itemgetter
import pprint
arr=[['0.55070000', '879.00000000'], ['0.55350000', '879.00000000'], ...]
sorted_arr = sorted(arr,key=lambda x:x[1])
pprint.pprint(sorted_arr)

CodePudding user response:

You are sorting the array with strings. Convert them to float in the lambda function and then try -

Note: Make sure to assign the output of the sorted function.

arr_sorted = sorted(arr, key=lambda x: float(x[1]))
arr_sorted
[['0.54490000', '94.00000000'],
 ['0.56040000', '96.00000000'],
 ['0.55070000', '879.00000000'],
 ['0.55350000', '879.00000000'],
 ['0.55640000', '879.00000000'],
 ['0.56210000', '879.00000000'],
 ['0.56490000', '879.00000000'],
 ['0.56780000', '879.00000000'],
 ['0.57060000', '879.00000000'],
 ['0.54500000', '899.00000000'],
 ['0.56370000', '901.00000000'],
 ['0.53070000', '908.00000000'],
 ['0.57350000', '916.00000000'],
 ['0.56300000', '939.00000000'],
 ['0.54960000', '999.00000000'],
 ['0.55000000', '9172.00000000']]

Add reverse=True in the sorted as a parameter if you want it sorted in descending order.


If you want to work on this array with more numeric operations after the sorting, then I would advise first converting the complete array to float values, and then doing the sorting OR other operations.

arr2 = [[float(j) for j in i] for i in arr]
arr_sorted = sorted(arr2, key=lambda x: x[1])
arr_sorted
[[0.5449, 94.0],
 [0.5604, 96.0],
 [0.5507, 879.0],
 [0.5535, 879.0],
 [0.5564, 879.0],
 [0.5621, 879.0],
 [0.5649, 879.0],
 [0.5678, 879.0],
 [0.5706, 879.0],
 [0.545, 899.0],
 [0.5637, 901.0],
 [0.5307, 908.0],
 [0.5735, 916.0],
 [0.563, 939.0],
 [0.5496, 999.0],
 [0.55, 9172.0]]
  • Related