[Tutor] order data

Sander Sweers sander.sweers at gmail.com
Fri Sep 18 22:40:16 CEST 2009


2009/9/18 Rayon <evosweet at hotmail.com>:

I will assume array == python list.

> I have a array with this data in it

How are you reading the data? Do you convert all the numbers to
floats? If so you will need to think about precision of the floats. If
you want to preserve the precision look into the decimal module.

> 0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 521 0.0919'],['IDT
> 521 0.095'],['None']
> 0.0083,0.0192,0.0109,39023821,['MCI 39023821 0.0109'],['ATT 39 0.012'],['IDT
> 39 0.0192'],['SPR 39 0.0135']
> 0.0421,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 0.026'],['IDT 73462
> 0.028'],['SPR 7 0.0681']
> 0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 1268 0.0919'],['IDT
> 126872 0.1035'],['None']
> 0.0215,0.1614,0.1399,5032130,['MCI 5032130 0.1614'],['ATT 5032130
> 0.1399'],['IDT 503 0.152'],['None']
> 0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868 0.0385'],['IDT 1868
> 0.036'],['None']
> 0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 5027889
> 0.0545'],['IDT 502 0.087'],['None']
> 0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 5027888
> 0.0545'],['IDT 502 0.087'],['None']
> 0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 521 0.0919'],['IDT
> 521 0.095'],['None']
> 0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 0.05'],['IDT 5989
> 0.173'],['SPR 598 0.047']
> 0.0706,0.2011,0.1305,1284499,['MCI 1284499 0.2011'],['ATT 1284499
> 0.1932'],['IDT 1284499 0.1305'],['None']
>
> and I want to order the display of that data by the second row.

You probably meant the second column here.If not please explain a bit more.

> can I do it in this array from or do I have to break it down more.

If the data is in a list of lists you can use itemgetter from the
operator module. Example below.

>>> import operator
>>> a = \
[[0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 521
0.0919'],['IDT 521 0.095'],['None']],
[0.0083,0.0192,0.0109,39023821,['MCI 39023821 0.0109'],['ATT 39
0.012'],['IDT 39 0.0192'],['SPR 39 0.0135']],
[0.0421,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 0.026'],['IDT
73462 0.028'],['SPR 7 0.0681']],
[0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 1268
0.0919'],['IDT 126872 0.1035'],['None']],
[0.0215,0.1614,0.1399,5032130,['MCI 5032130 0.1614'],['ATT 5032130
0.1399'],['IDT 503 0.152'],['None']],
[0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868
0.0385'],['IDT 1868 0.036'],['None']],
[0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 5027889
0.0545'],['IDT 502 0.087'],['None']],
[0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 5027888
0.0545'],['IDT 502 0.087'],['None']],
[0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 521
0.0919'],['IDT 521 0.095'],['None']],
[0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 0.05'],['IDT
5989 0.173'],['SPR 598 0.047']],
[0.0706,0.2011,0.1305,1284499,['MCI 1284499 0.2011'],['ATT 1284499
0.1932'],['IDT 1284499 0.1305'],['None']]]
>>> a.sort(key=operator.itemgetter(1))
>>> for l in a: print l

[0.0083000000000000001, 0.019199999999999998, 0.0109, 39023821, ['MCI
39023821 0.0109'], ['ATT 39 0.012'], ['IDT 39 0.0192'], ['SPR 39
0.0135']]
[0.0206, 0.0385, 0.017899999999999999, 1868, ['MCI 1868 0.0179'],
['ATT 1868 0.0385'], ['IDT 1868 0.036'], ['None']]
[0.042099999999999999, 0.068099999999999994, 0.025999999999999999,
73462, ['MCI 73462 0.0260'], ['ATT 7 0.026'], ['IDT 73462 0.028'],
['SPR 7 0.0681']]
[0.032500000000000001, 0.086999999999999994, 0.0545, 5027889, ['MCI
5027889 0.0602'], ['ATT 5027889 0.0545'], ['IDT 502 0.087'], ['None']]
[0.032500000000000001, 0.086999999999999994, 0.0545, 5027888, ['MCI
5027888 0.0602'], ['ATT 5027888 0.0545'], ['IDT 502 0.087'], ['None']]
[0.0045999999999999999, 0.095000000000000001, 0.090399999999999994,
521456, ['MCI 521456 0.0904'], ['ATT 521 0.0919'], ['IDT 521 0.095'],
['None']]
[0.0045999999999999999, 0.095000000000000001, 0.090399999999999994,
521455, ['MCI 521455 0.0904'], ['ATT 521 0.0919'], ['IDT 521 0.095'],
['None']]
[0.017600000000000001, 0.10349999999999999, 0.085900000000000004,
126872, ['MCI 126872 0.0859'], ['ATT 1268 0.0919'], ['IDT 126872
0.1035'], ['None']]
[0.021499999999999998, 0.16139999999999999, 0.1399, 5032130, ['MCI
5032130 0.1614'], ['ATT 5032130 0.1399'], ['IDT 503 0.152'], ['None']]
[0.12920000000000001, 0.1762, 0.047, 5989, ['MCI 5989 0.1762'], ['ATT
598 0.05'], ['IDT 5989 0.173'], ['SPR 598 0.047']]
[0.070599999999999996, 0.2011, 0.1305, 1284499, ['MCI 1284499
0.2011'], ['ATT 1284499 0.1932'], ['IDT 1284499 0.1305'], ['None']]
>>>

As you can see the numbers are converted to floats in my example and
0.0192 changed to 0.019199999999999998.

Greets
Sander


More information about the Tutor mailing list