Wednesday 6 July 2011

Python using pylab (Sorting)

I use ipython for my testing purpose, here are some code I try on ipython:

I have pylab installed for my vector/matrix and math calculations;

first import pylab:

>>> import pylab

then load some file, my file is (geomtable.txt) formatted like this (it is a seismic coordinate/geometry table):


1 206 659 709842.0 9311099.0 10.99 -136.215
2 207 658 709842.0 9311099.0 10.99 -165.6061
3 207 658 709829.0 9311071.0 10.99 -134.7688
4 208 657 709829.0 9311071.0 10.99 -165.412
5 208 657 709819.0 9311044.0 11.1 -136.6259
6 209 656 709819.0 9311044.0 11.1 -166.709
7 209 656 709809.0 9311015.0 11.16 -136.0713
8 205 660 709752.0 9310875.0 11.13 136.2151
9 205 660 709747.0 9310844.0 11.1 -135.1495
10 205 660 709728.0 9310819.0 11.48 136.2746

use this command:

>>> rcvpos = pylab.loadtxt("geomtable.txt")

your table will be loaded, next step is sorting the table according to specified column in the table, in my case I will sort the table according to column 3 & 4 (that is UTM coordinate)

use this command to sort:

>>> sorted(rcvpos, key=lambda item:item[3])

in my case I will get minimum and maximum value of column 3 and 4 respectively, use these code below:

>>> minx = sorted(rcvpos, key=lambda item:item[3])[0][3]

>>> miny = sorted(rcvpos, key=lambda item:item[4])[0][4]

>>> maxx = sorted(rcvpos, key=lambda item:item[3])[1334][3]

>>> maxy = sorted(rcvpos, key=lambda item:item[4])[1334][4]

hope this helpful.

No comments:

Post a Comment