numpy arrays

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Apr 7 10:48:29 EDT 2016


On 7 April 2016 at 15:31, Heli <hemla21 at gmail.com> wrote:
>
> Thanks a lot Oscar,
>
> The lexsort you suggested was the way to go.

Glad to hear it.

> import h5py
> import numpy as np
> f=np.loadtxt(inputFile,delimiter=None)
> xcoord=np.sort(np.unique(f[:,0]))
> ycoord=np.sort(np.unique(f[:,1]))
> zcoord=np.sort(np.unique(f[:,2]))
>
> x=f[:,0]
> y=f[:,1]
> z=f[:,2]
> val=f[:,3]
>
> ind = np.lexsort((val,z,y,x)) # Sort by x, then by y, then by z, then by val
> sortedVal=np.array([(val[i]) for i in ind]).reshape((xcoord.size,ycoord.size,zcoord.size))

One final possible improvement is that

     np.array([val[i] for i in ind])

can probably be done with fancy indexing

    val[ind,:]

removing the only Python loop left.

--
Oscar



More information about the Python-list mailing list