FAQ
I'm having trouble sorting a dictionary based on values when the values are
all lists, and i want to sort the list by key with the largest value lists
in decreasing size.

Currently, I have the following:

from operator import itemgetter

dict = {'A': [(10, 20), (12, 18), (5, 11), (18, 25)], 'C': [(1, 200)], 'B':
[(1, 10), (100, 200), (150, 300), (250, 350), (300, 400)], 'D': [(3, 400)]}

I have tried several methods, and seem to have come closest using:

sorted(self.dict.items(), key=itemgetter(1), reverse=True)

My problem is that this will return the key order A,D,C,B
The order I want is based on the size of the lists each key points to:
B,A,D,C or B,A,C,D

Question:
itemgetter(1) is just returning the value, which is a list. How do I
specify that I want the values to be sorted by list size?

Thanks!
john





--
"We are healthy only to the extent that our ideas are humane." --Killgore
Trout
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081114/f42e65e2/attachment.htm>

Search Discussions

  • Vlastimil Brom at Nov 14, 2008 at 7:10 pm
    2008/11/14 major-john <iamh2o at gmail.com>
    I'm having trouble sorting a dictionary based on values when the values are
    all lists, and i want to sort the list by key with the largest value lists
    in decreasing size.

    Currently, I have the following:

    from operator import itemgetter

    dict = {'A': [(10, 20), (12, 18), (5, 11), (18, 25)], 'C': [(1, 200)], 'B':
    [(1, 10), (100, 200), (150, 300), (250, 350), (300, 400)], 'D': [(3, 400)]}

    I have tried several methods, and seem to have come closest using:

    sorted(self.dict.items(), key=itemgetter(1), reverse=True)

    My problem is that this will return the key order A,D,C,B
    The order I want is based on the size of the lists each key points to:
    B,A,D,C or B,A,C,D

    Question:
    itemgetter(1) is just returning the value, which is a list. How do I
    specify that I want the values to be sorted by list size?

    Thanks!
    john





    --
    "We are healthy only to the extent that our ideas are humane." --Killgore
    Trout

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

    You may try e.g.:
    sorted(d.items(), key=lambda x: max(x[1]), reverse=True)
    [('B', [(1, 10), (100, 200), (150, 300), (250, 350), (300, 400)]), ('A',
    [(10, 20), (12, 18), (5, 11), (18, 25)]), ('D', [(3, 400)]), ('C', [(1,
    200)])]


    (the dict is named d here in order not to shadow the builtin)
    The ordering of items containing the same highest value is undefined
    here.
    hth,
    vbr
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20081114/2b485e17/attachment-0001.htm>
  • Chris Rebert at Nov 14, 2008 at 8:10 pm

    On Fri, Nov 14, 2008 at 10:26 AM, major-john wrote:
    I'm having trouble sorting a dictionary based on values when the values are
    all lists, and i want to sort the list by key with the largest value lists
    in decreasing size.

    Currently, I have the following:

    from operator import itemgetter

    dict = {'A': [(10, 20), (12, 18), (5, 11), (18, 25)], 'C': [(1, 200)], 'B':
    [(1, 10), (100, 200), (150, 300), (250, 350), (300, 400)], 'D': [(3, 400)]}

    I have tried several methods, and seem to have come closest using:

    sorted(self.dict.items(), key=itemgetter(1), reverse=True)

    My problem is that this will return the key order A,D,C,B
    The order I want is based on the size of the lists each key points to:
    B,A,D,C or B,A,C,D

    Question:
    itemgetter(1) is just returning the value, which is a list. How do I
    specify that I want the values to be sorted by list size?
    You just need to add a call to len() in the key function:

    sorted(self.dict.items(), key=lambda pair: len(pair[1]), reverse=True)

    Cheers,
    Chris
    --
    Follow the path of the Iguana...
    http://rebertia.com
    Thanks!
    john





    --
    "We are healthy only to the extent that our ideas are humane." --Killgore
    Trout

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 14, '08 at 6:26p
activeNov 14, '08 at 8:10p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2023 Grokbase