Initialize numpy array using in-place sorted list

I'm observing some odd behavior. Here is the snippet of code:

>>> import numpy as np
>>> a = [[1, .3], [0, .5], [2, .23]]
>>> b = np.array(a.sort())
>>> b
array(None, dtype=object)

Is this behavior expected? If I add an intermediate step for the in-place sort, it works as expected:

>>> a = [[1, .3], [0, .5], [2, .23]]
>>> a.sort()
>>> b = np.array(a)
>>> b
array([[ 0.  ,  0.5 ],
       [ 1.  ,  0.3 ],
       [ 2.  ,  0.23]])

Can someone explain what is happening?

Answers


The issue is that a.sort() does not return the sorted list. It returns None:

>>> a.sort() is None
True

You could use sorted(a):

>>> b = np.array(sorted(a))
>>> b
array([[ 0.  ,  0.5 ],
       [ 1.  ,  0.3 ],
       [ 2.  ,  0.23]])

However, this would create a (sorted) copy of a.


Need Your Help

Redirecting multiple stdouts to single file

linux command-line stdout nfs

I have a program running on multiple machines with NFS and I'd like to log all their outputs into a single file. Can I just run ./my_program >> filename on every machine or is there an issue ...

Most simple code to populate JTable from ResultSet

java swing sqlite jtable defaulttablemodel

I googled the whole day and no luck. I call getnPrintAllData() method after pressing OK button. So the code is: