Python3: Using sorted(key=...)

Johannes Bauer dfnsonfsduifb at gmx.de
Thu Aug 6 22:15:52 EDT 2009


MRAB schrieb:
> Johannes Bauer wrote:
>> Hello list,
>>
>> I'm having trouble with a incredibly simple sort of a list containing
>> ints and tuples:
>>
>> def myorder(x):
>>     if type(x) == int:
>>         return x
>>     else:
>>         return x[0]
>>
>> odata = sorted([ (a, b) for (a, b) in data["description"].items() ],
>> key=myorder)
>>
> You're sorting a list of tuples (key/value pairs), so 'myorder' is
> always given a tuple.

Oh good lord! You're right... I meant

def myorder(x):
     if type(x[0]) == int:
         return x[0]
     else:
         return x[0][0]

Thanks for your help,
Kind regards,
Johannes

-- 
"Du bist einfach nur lächerlich! Mit solchen albernen und hohlen Sätzen
kannst du mir nicht imprägnieren."
-- Hobbycholeriker Jens Fittig aka Wolfgang Gerber in de.sci.electronics
                             <4a6f44d0$0$12481$9b622d9e at news.freenet.de>



More information about the Python-list mailing list