[Tutor] Unique Items in Lists

Chad Crabtree flaxeater at yahoo.com
Thu Jan 27 21:39:12 CET 2005


Try a simple bubble sort.  This will not have great performance on
big 
files at a worst case scenario it will take n-1 loops to sort.  This
is 
not very general and would require a substantially different
implementation.

def bublesort(l1,l2,l3):
    modified=True
    while modified:
        modified=False
        for number,value in enumerate(l3):
            if number>0:#to avoid index error
                if l3[number]<l3[number-1]:
                    #move everything down one
                   
temp1,temp2,temp3=l1[number],l2[number],l3[number]
                    
l1[number],l2[number],l3[number]=l1[number-1],l2[number-1],l3[number-1]
                   
l1[number-1],l2[number-1],l3[number-1]=temp1,temp2,temp3
                    modified=True
    for number,value in enumerate(l1):
        print l1[number],l2[number],l3[number]


bublesort([1,2,3,4],
          ['a','b','d','c'],
          ['yorkshire','detroit','kalamazoo','chicago'])

Actually as I was writing this I thought of a better way using sort.

l=[[1,2,3,4],
   ['a','b','d','c'],
   ['yorkshire','detroit','kalamazoo','chicago']]
print l
l=zip(l[0],l[1],l[2])
l.sort(lambda x,y:cmp(x[2],y[2]))
for x in l:
    for y in x:
        print y,
    print


Srinivas Iyyer wrote:

>Dear Danny, thank you for ur help. But a basic
>question ?
>
>In a table, more specifically a matrix 3X3,
>
>Apple    Fruit    Denmark
>F-16     Fighter  USA
>Taj      Wonder   India
>Mummy    Antique  Egypt
>
>
>IF I have to sort on country, it should be
>
>Apple    Fruit    Denmark
>Mummy    Antique  Egypt
>Taj      Wonder   India
>F-16     Fighter  USA
>  
>


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250


More information about the Tutor mailing list