[Tutor] list sort problem

Dave Angel davea at ieee.org
Sun Sep 20 01:46:35 CEST 2009


Rayon wrote:
> ok so here it is I think  this one should be very clear:
>
> I have some data in a list, the data in question: 
>
> 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.042,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']
> 0.0325,0.087,0.0545,5027881,['MCI 5027881 0.0602'],['ATT 5027881 0.0545'],['IDT 502 0.087'],['None']
>
> my problem is that I want to order it by the first item so for line number one that would be  0.0046: 
> this value is not unique it might repeat, if the last 10 values like this that might be the same and there are the lowest in the over list. 
> I want them first when I display this list and the bigger  values after in order is size.  
>
>
>   
I'll have to make some assumptions, please correct or confirm them.

Each line of your message represents one item in a list.  Each such item 
is in turn a sublist.

Each of these sublists contains 3 floats, an int, and a few more subsublists

Each of these subsublists contains a string

Anyway, you want to sort the top-level list.  Since the first item in 
each sublist is a float, by default they will be ordered by the first 
item, which is what you're asking for.  In case of a collision, the 
second item is examined, and so on.

      mylist.sort()
will sort the list in-place, based on the first element of each item in 
the list


mylist = []
mylist.append( [0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 
521 0.0919'],['IDT 521 0.095'],['None']])
mylist.append( [0.0083,0.0192,0.0109,39023821,['MCI 39023821 
0.0109'],['ATT 39 0.012'],['IDT 39 0.0192'],['SPR 39 0.0135']])
mylist.append( [0.042,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 
0.026'],['IDT 73462 0.028'],['SPR 7 0.0681']])
mylist.append( [0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 
1268 0.0919'],['IDT 126872 0.1035'],['None']])
mylist.append( [0.0215,0.1614,0.1399,5032130,['MCI 5032130 
0.1614'],['ATT 5032130 0.1399'],['IDT 503 0.152'],['None']])
mylist.append( [0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868 
0.0385'],['IDT 1868 0.036'],['None']])
mylist.append( [0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 
5027889 0.0545'],['IDT 502 0.087'],['None']])
mylist.append( [0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 
5027888 0.0545'],['IDT 502 0.087'],['None']])
mylist.append( [0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 
521 0.0919'],['IDT 521 0.095'],['None']])
mylist.append( [0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 
0.05'],['IDT 5989 0.173'],['SPR 598 0.047']])
mylist.append( [0.0706,0.2011,0.1305,1284499,['MCI 1284499 
0.2011'],['ATT 1284499 0.1932'],['IDT 1284499 0.1305'],['None']])
mylist.append( [0.0325,0.087,0.0545,5027881,['MCI 5027881 0.0602'],['ATT 
5027881 0.0545'],['IDT 502 0.087'],['None']])

def show_list_partial(thelist):
    for item in thelist:
        print item[0], item[1]

show_list_partial(mylist)
mylist.sort()
print "--------------------------------"
show_list_partial(mylist)


DaveA.




More information about the Tutor mailing list