0

As following my previous question. I now have a ndarray with the form:

 [[  0.        ]
 [ 18.21545873]
 [ 14.93752607]
 [ 15.55222205]
 [ 14.93752607]
 [ 10.68406032]
 [ 11.54598659]
 [ 15.55222205]
 [ 11.54598659]
 [ 12.30302814]]
  1. How do I sort this array? Since it is not a normal list. It is a ndarray, in other words, it is a list of lists.
  2. Is normal that people sort this list of lists directly, or people usually change it to a normal list

    [ 0. , 18.21545873, 14.93752607, 15.55222205, 14.93752607, 10.68406032, 11.54598659, 15.55222205, 11.54598659, 12.30302814]

and then sort this list?

Many thanks!

askewchan
  • 45,161
  • 17
  • 118
  • 134
Murphy
  • 13
  • 3
  • 1
    You can sort a 2D numpy array by a specific column. In your case, this will simply be column `0`. See here http://stackoverflow.com/a/2828371/2296458 – Cory Kramer Apr 04 '14 at 20:00
  • Thanks @Cyber, I just found out this, and applied to my case. But still, about my second question: usually do people treat this kind of 1d array as a "1d ndarray", or they would change it to a list? And how can we change this 1d array as a list? Thanks! – Murphy Apr 04 '14 at 20:18
  • it depends on your intentions. If your 2D array will only ever have one element in each array, then of course you only need a 1D array. To answer your other question, the way you turn a numpy array to a list is: say your numpy array is `numpyArray`, you just say `newList = list(numpyArray)` – Cory Kramer Apr 04 '14 at 20:21
  • Hi @Cyber, what I got from `newList = list(numpyArray)`, is `array([0.]), array([18.21545873]), ...`. But what I meant is `[0., 18.21545873, ...]`. Do I need to open another question for this? – Murphy Apr 04 '14 at 20:32
  • sorry my mistake, need some more coffee ;) say your numpy array was named `x`, try this: `[i for i in x[:,0]]` – Cory Kramer Apr 04 '14 at 20:38
  • It is faster and better to do `numpy_array.tolist()`, or if you want it as a flat list, `numpy_array.ravel().tolist()`. But there are few reasons why you would want to turn a numpy array into a list, especially if what you want is to sort it. – Jaime Apr 04 '14 at 21:12
  • Thanks for both @Cyber and @Jaime. Cyber's method and Jaime's second way worked for me. But for Jaime's first method, did you mean the `numpy.ndarray.tolist()` function? This function gives me still a "list of lists" in the form of `[[1], [2], [3], ...]`, instead of the one I want: `[ 1, 2, 3,...]`. And, Jaime, do you mean that usually people would change a ndarray into a list, especially when they want to sort it? Thank you! – Murphy Apr 04 '14 at 22:27

1 Answers1

1

Nothing complicated.

l = [[  0.        ],
     [ 18.21545873],
     [ 14.93752607],
     [ 15.55222205],
     [ 14.93752607],
     [ 10.68406032],
     [ 11.54598659],
     [ 15.55222205],
     [ 11.54598659],
     [ 12.30302814]]
l.sort()

produces what you want. Maybe I'm missing what the problem is.

Octopus
  • 8,075
  • 5
  • 46
  • 66
  • thank you! But when I tried this, I got "None". Is this related to the fact that my 1d array does not have "," as a separator? And as my second question in my original post, do you know that usually people use "ndarrays" or "list of lists"? And is there any way to transfer a ndarray to a list? Thanks! – Murphy Apr 04 '14 at 20:21
  • 1
    @Murphy `sort()` does not return anything, so you cannot say `newArray = l.sort()`. It will be sorting the array `l` in place, so if you print `l` after you `sort()` it, you will notice it has been sorted. – Cory Kramer Apr 04 '14 at 20:23
  • @Murphy, if you want to get the returned value, you can perhaps use `new = sorted(l)` – Saullo G. P. Castro Apr 05 '14 at 11:28