Hi, I am getting the following errorTypeError: sort() takes no keyword arguments

Duncan Booth duncan.booth at invalid.invalid
Fri Oct 17 03:54:00 EDT 2008


Aidan <aweraw at gmail.com> wrote:

> gaurav kashyap wrote:
>> Hi all,
>> I am using python version 2.3.in a program ,
>> I have called the sort function.Wherein,
>> a.sort(reverse=True)
>> is giving the following error:
>> 
>> TypeError: sort() takes no keyword arguments.
>> 
>> It works in python 2.4,What can be the alternative in python 2.3
>> 
>> Thanks ,
>> Gaurav
>> 
> 
> You could try:
> 
> >>> a.sort()
> >>> a = a[::-1]
> 
> 
You could, but you should also be aware that there is a subtle difference. 
The 'reverse' keyword argument gives a stable reverse sort, sorting and 
then reversing something is not stable. e.g.

>>> data = [(1, 'zzz'), (1, 'abc'), (2, 'def'), (1, 'pqr'), (2, 'jkl')]
>>> a = data[:]
>>> a.sort(lambda p,q: cmp(p[0],q[0]), reverse=True)
>>> a
[(2, 'def'), (2, 'jkl'), (1, 'zzz'), (1, 'abc'), (1, 'pqr')]
>>> a = data[:]
>>> a.sort(lambda p,q: cmp(p[0],q[0]))
>>> a[::-1]
[(2, 'jkl'), (2, 'def'), (1, 'pqr'), (1, 'abc'), (1, 'zzz')]


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list