Re: Sort the values of a dict

2009-12-19 Thread Rhodri James
On Fri, 18 Dec 2009 23:00:42 -, David Robinow wrote: I won't engage in any arguments about pythonicity but it seems simpler if you convert to a list of tuples right away. d = {1:('a', 1, 12), 5:('r',21,10), 2:('u',9,8)} l = [(x, d[x]) for x in d.keys()] which is a long way of writing "

Re: Sort the values of a dict

2009-12-19 Thread John Bokma
Rory Campbell-Lange writes: > I'm a bit rusty, and have just picked up the new "Learning Python" book > but I'm finding that it isn't providing a useful refresher to the > language or a particularly good introduction to version 3. Have you any > suggestions. While you aren't addressing me, I do

Re: Sort the values of a dict

2009-12-19 Thread Rory Campbell-Lange
> mylist = [z for z in zip(list(d), list(d.values()))] > The Pythonic way for the above line is: > >>> mylist = list(d.items()) Quite right. Thanks for pointing that out. I liked Chris Kaynor's solution: s = sorted(d.items(), key=lambda i: i[1][2]) I'm a bit rusty, and have just picked up

Re: Sort the values of a dict

2009-12-19 Thread mattia
Il Sat, 19 Dec 2009 17:30:27 +1100, Lie Ryan ha scritto: > On 12/19/2009 9:34 AM, mattia wrote: >> Can you provide me a much pythonic solution (with comments if possible, >> so I can actually learn something)? > > If you only need to get i'th element sometimes, sorting the dict is > fine. Otherwi

Re: Sort the values of a dict

2009-12-18 Thread Lie Ryan
On 12/19/2009 9:34 AM, mattia wrote: Can you provide me a much pythonic solution (with comments if possible, so I can actually learn something)? If you only need to get i'th element sometimes, sorting the dict is fine. Otherwise, you might want to use collections.OrderedDict. -- http://mail.p

Re: Sort the values of a dict

2009-12-18 Thread MRAB
Rory Campbell-Lange wrote: 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

Re: Sort the values of a dict

2009-12-18 Thread Rory Campbell-Lange
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

Re: Sort the values of a dict

2009-12-18 Thread mattia
Il Fri, 18 Dec 2009 18:00:42 -0500, David Robinow ha scritto: > On Fri, Dec 18, 2009 at 5:34 PM, mattia 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 t

Re: Sort the values of a dict

2009-12-18 Thread David Robinow
On Fri, Dec 18, 2009 at 5:34 PM, mattia 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 l

Re: Sort the values of a dict

2009-12-18 Thread mattia
Actually, in order to use duplicate values I need something like: >>> import copy >>> d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8), 3:('u', 9, 8) } >>> dc = copy.deepcopy(d) >>> t = [x for x in d.values()] >>> def third(mls): ... return mls[2] ... >>> s = sorted(t, key=third) >>> pres =

Re: Sort the values of a dict

2009-12-18 Thread Chris Kaynor
I'd write it as: s = sorted(d.iteritems(), key=lambda i: i[1][2]) If using python 3, it should be d.items() instead of d.iteritems(). d.iteritems() is a generator yielding tuples of (key, value) from the dictionary 'd'. lambda i: i[1][2] is the same as: def sort_(i): return i[1][2] but in-lin

Sort the values of a dict

2009-12-18 Thread mattia
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