newbie question

Classified stephenxbchen at hotmail.com
Sat Dec 14 12:18:39 EST 2002


On Sat, 14 Dec 2002 13:00:43 +0000, Justin Shaw wrote:

> Is this what you mean?  Note j's range.
> 
>> a=[('a',1),('c',5),('k',5),('o',3), ('i',3)]
>> for i in range(0, len(a)-1):
>>     for j in range(i+1, len(a)):
>>         if a[i][1]>a[j][1]:
>>             print a
>>             print '    swap', i, j, a[i], a[j]
>>             a[i],a[j]=a[j],a[i]
>>             print '   ', a
>> print a
> 
> RESULT
> [('a', 1), ('c', 5), ('k', 5), ('o', 3), ('i', 3)]
>     swap 1 3 ('c', 5) ('o', 3)
>     [('a', 1), ('o', 3), ('k', 5), ('c', 5), ('i', 3)]
> [('a', 1), ('o', 3), ('k', 5), ('c', 5), ('i', 3)]
>     swap 2 4 ('k', 5) ('i', 3)
>     [('a', 1), ('o', 3), ('i', 3), ('c', 5), ('k', 5)]
> [('a', 1), ('o', 3), ('i', 3), ('c', 5), ('k', 5)]
Oh, I got the bubble sort wrong...shame on me...
thanks for your help and the tips on debugger...



More information about the Python-list mailing list