Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sort the values of a dict

0 views
Skip to first unread message

mattia

unread,
Dec 18, 2009, 5:34:53 PM12/18/09
to
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))]
>>>

Can you provide me a much pythonic solution (with comments if possible,
so I can actually learn something)?

Thanks, Mattia

mattia

unread,
Dec 18, 2009, 5:42:52 PM12/18/09
to
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 = []
>>> for x in s:
... for k in d.keys():
... if d[k] == x:
... pres.append(k)

... del d[k] # speedup and use duplicate values


... break
...
>>> res = []
>>> for x in pres:

... res.append((x, dc[x]))
...
>>> res
[(2, ('u', 9, 8)), (3, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1,
12))]
>>>

David Robinow

unread,
Dec 18, 2009, 6:00:42 PM12/18/09
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>
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()]
def third(q):
return q[1][2]

s = sorted(l, key=third)
print s

mattia

unread,
Dec 18, 2009, 6:20:51 PM12/18/09
to

Thanks, I'm not yet aware of all the wonderful conversions python can do,
amazing.

Rory Campbell-Lange

unread,
Dec 18, 2009, 7:00:54 PM12/18/09
to mattia, pytho...@python.org

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
ro...@campbell-lange.net

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928

MRAB

unread,
Dec 18, 2009, 7:19:15 PM12/18/09
to pytho...@python.org
Rory Campbell-Lange wrote:
> On 18/12/09, mattia (ger...@gmail.com) wrote:
> How about
>
>>>> mylist = [z for z in zip(list(d), list(d.values()))]
>
The Pythonic way for the above line is:

>>> mylist = list(d.items())

Lie Ryan

unread,
Dec 19, 2009, 1:30:27 AM12/19/09
to
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.

mattia

unread,
Dec 19, 2009, 4:50:19 AM12/19/09
to

Well, in the python doc OrderedDict is described as a dict that remembers
the order that keys were first inserted and I don't need this. The fact
is that I use a structure composed by a date and a list of possible
solutions found, like
(2009/12/21, (('e', 12, 33), ('r', 4, 11), ('r', 1, 33))) then every
solution is inserted concurrently in a dictionary. I want to sort the
solution found to provide, e.g., the first 10 dates found and the best
result of every date based on the i-th element of the date's list.

Rory Campbell-Lange

unread,
Dec 19, 2009, 5:43:31 PM12/19/09
to MRAB, pytho...@python.org
> >>>>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 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.

Regards
Rory

--
Rory Campbell-Lange

John Bokma

unread,
Dec 19, 2009, 6:09:01 PM12/19/09
to
Rory Campbell-Lange <ro...@campbell-lange.net> 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 have 2 suggestions:

* Programming in Python 3 by Mark Summerfield (make sure you get the
2nd edition)
* Dive into Python 3 by Mark Pilgrim

I am halfway in the first book (but the 1st edition) and like it a
lot. I know Dive into Python from an earlier version (2.x) and have no
problem at all recommending the Python 3 edition. You can read/download it
here: http://diveintopython3.org/

--
John Bokma

Read my blog: http://johnbokma.com/
Hire me (Perl/Python): http://castleamber.com/

Rhodri James

unread,
Dec 19, 2009, 8:18:40 PM12/19/09
to
On Fri, 18 Dec 2009 23:00:42 -0000, David Robinow <drob...@gmail.com>
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 "l = d.items()", or "l = list(d.items())"
if you're using Python 3. :-)

> def third(q):
> return q[1][2]
>
> s = sorted(l, key=third)
> print s


--
Rhodri James *-* Wildebeeste Herder to the Masses

0 new messages