Title: RE: Sorting dictionary by 'sub' value

[Rory Campbell-Lange]

#- I have a dictionary of images. I wish to sort the dictionary 'v' by a
#- dictionary value using python 2.3. The dictionary value is the date
#- attribute as shown here:

You have to use the DSU algorithm (Decorate, Sort, Undecorate), taking in consideration that the final sorted element can not be a dictionary. For example (with a simpler dictionary):

>>> d = {'a': ('m', 5), 'b': ('k', 9), 'c': ('f', 2)}

I want the dictionary values sorted by the number inside the tuple. So I prepare a list with that number first, and the dictionary key in second place:

>>> temp_list = [ (x[1][1], x[0]) for x in d.items() ]

Then, just sort it and use it.

>>> temp_list.sort()
>>> for (tmp, key) in temp_list:
        print d[key]
       
('f', 2)
('m', 5)
('k', 9)
>>>


.    Facundo

Bitácora De Vuelo: http://www.taniquetil.com.ar/plog
PyAr - Python Argentina: http://pyar.decode.com.ar/


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ADVERTENCIA.

La información contenida en este mensaje y cualquier archivo anexo al mismo, son para uso exclusivo del destinatario y pueden contener información confidencial o propietaria, cuya divulgación es sancionada por la ley.

Si Ud. No es uno de los destinatarios consignados o la persona responsable de hacer llegar este mensaje a los destinatarios consignados, no está autorizado a divulgar, copiar, distribuir o retener información (o parte de ella) contenida en este mensaje. Por favor notifíquenos respondiendo al remitente, borre el mensaje original y borre las copias (impresas o grabadas en cualquier medio magnético) que pueda haber realizado del mismo.

Todas las opiniones contenidas en este mail son propias del autor del mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones Personales S.A. o alguna empresa asociada.

Los mensajes electrónicos pueden ser alterados, motivo por el cual Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación cualquiera sea el resultante de este mensaje.

Muchas Gracias.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to