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