On 18/12/09, mattia (ger...@gmail.com) wrote: > Hi all, I have a dictionary that uses dates and a tuples ad key, value > pairs. I need to sort the values of the dict and insert everything in a > tuple. The additional problem is that I need to sort the values looking > at the i-th element of the list. I'm not that good at python (v3.1), but > this is my solution: > > >>> d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)} > >>> t = [x for x in d.values()] > >>> def third(mls): > ... return mls[2] > ... > >>> s = sorted(t, key=third) > >>> pres = [] > >>> for x in s: > ... for k in d.keys(): > ... if d[k] == x: > ... pres.append(k) > ... break > ... > >>> res = [] > >>> for x in pres: > ... res.append((x, d[x])) > ... > >>> res > [(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))]
How about >>> mylist = [z for z in zip(list(d), list(d.values()))] and then sort on your sort criteria, either i[0], i[0][1], i[0][2] or i[0][3]. Rory -- Rory Campbell-Lange Director r...@campbell-lange.net Campbell-Lange Workshop www.campbell-lange.net 0207 6311 555 3 Tottenham Street London W1T 2AF Registered in England No. 04551928 -- http://mail.python.org/mailman/listinfo/python-list