[Tutor] Sorting a list
Walker Hale IV
walker.hale.iv at gmail.com
Thu May 14 21:46:29 CEST 2009
The prize goes to Bob Gailer! This solution is not only shortest, but
works perfectly with negative numbers.
--
Walker Hale <walker.hale.iv at gmail.com>
On Thu, May 14, 2009 at 1:22 PM, bob gailer <bgailer at gmail.com> wrote:
> Lie Ryan wrote:
>>
>> Timo wrote:
>>>
>>> Hello,
>>>
>>> I don't think this should be difficult, so maybe I look over it. But I
>>> can't seem to find the solution.
>>>
>>> I have a list with one word and a couple of numbers. Now I want the word
>>> to be kept in the first location and the numbers to be sorted.
>>>
>>> So this:
>>> [4, 6, 'word', 3, 9]
>>>
>>> should be:
>>>
>>> ['word', 3, 4, 6, 9]
>>>
>>
>> Why not:
>> >>> sorted(lst, key=lambda x: (0, x) if isinstance(x, str) else (1, x))
>> ['word', 3, 4, 6, 9]
>
> Again in the relentless pursuit of terseness:
>
> sorted(a, key=lambda x: (isinstance(x, int), x))
>
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list