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!

7
  • 1
    You can sort a 2D numpy array by a specific column. In your case, this will simply be column 0. See here stackoverflow.com/a/2828371/2296458 Commented Apr 4, 2014 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! Commented Apr 4, 2014 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) Commented Apr 4, 2014 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? Commented Apr 4, 2014 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]] Commented Apr 4, 2014 at 20:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
@Murphy, if you want to get the returned value, you can perhaps use new = sorted(l)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.