Sorting using lambda not working in Py2.1?
Oleg Broytmann
phd at phd.fep.ru
Thu May 3 12:42:17 EDT 2001
Hi!
I am impressed by the number of mistakes you did! :)
On Thu, 3 May 2001, mary wrote:
> I may be doing something wrong, or is this a bug in 2.1?
>
> x = (9,5,4,3,1,2)
>
> print x.sort()
AttributeError: x has no attribute "sort".
I think "x" is a list:
x = [9,5,4,3,1,2]
> -> (1,2,3,4,5,9)
x.sort() sorts in-place and returns None. So this print prints None. You
want to print x, not x.sort().
> print x.sort(lamda x,y: x<y)
> -> (9,5,4,3,1,2) !!
Hmm...
>>> x = [9,5,4,3,1,2]
>>> x.sort(lambda x,y: x<y)
>>> print x
[9, 5, 4, 3, 1, 2]
>>> x.sort(lambda x,y: cmp(x, y))
>>> print x
[1, 2, 3, 4, 5, 9]
>>> x.sort(lambda x,y: x>y)
>>> print x
[1, 2, 3, 4, 5, 9]
Something strange here...
Oleg.
----
Oleg Broytmann http://phd.pp.ru/ phd at phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-list
mailing list