Re: Oddity using sorted with key

2014-03-11 Thread Josh English
On Tuesday, March 11, 2014 9:25:29 AM UTC-7, John Gordon wrote: > > > > > Why do you say that 'key=lambda x: x.name.lower' is the correct form? That > > returns the str.lower() function object, which is a silly thing to sort > > on. Surely you want to sort on the *result* of that function, w

Re: Oddity using sorted with key

2014-03-11 Thread Josh English
A comprehensive and educational answer, Peter. Thank you. Josh -- https://mail.python.org/mailman/listinfo/python-list

Re: Oddity using sorted with key

2014-03-11 Thread Peter Otten
Josh English wrote: > I am running into a strange behavior using the sorted function in Python > print list(sorted(all_the_stuff, key=lambda x: x.name.lower)) > print list(sorted(all_the_stuff, key=lambda x: x.name.lower())) Let's simplify your example some more: >>> items = ["B", "2", "a"] >>>

Re: Oddity using sorted with key

2014-03-11 Thread John Gordon
In <058a4a9e-7893-44ef-97c0-999a3589e...@googlegroups.com> Josh English writes: > print list(sorted(all_the_stuff, key=lambda x: x.name.lower)) > print list(sorted(all_the_stuff, key=lambda x: x.name.lower())) > # END > The output is: > [Thing d, Thing f, Thing 2, Thing a, Thing b, Thing C] >

Re: Oddity using sorted with key

2014-03-11 Thread emile
On 03/11/2014 09:13 AM, Josh English wrote: I am running into a strange behavior using the sorted function in Python 2.7. The key parameter is not behaving as the docs say it does: Here is a snippet of code, simplified from my full program: #begin code class Thing(object): def __init__(se

Re: Oddity using sorted with key

2014-03-11 Thread Chris Kaynor
On Tue, Mar 11, 2014 at 9:13 AM, Josh English wrote: > print list(sorted(all_the_stuff, key=lambda x: x.name.lower)) > In this case, the key being sorted on is the function object x.name.lower, not the result of the call. It might make more sense if you break the lambda out into a separate def s

Oddity using sorted with key

2014-03-11 Thread Josh English
I am running into a strange behavior using the sorted function in Python 2.7. The key parameter is not behaving as the docs say it does: Here is a snippet of code, simplified from my full program: #begin code class Thing(object): def __init__(self, name): self.name = name def __