Sort a Dictionary

Discussion in 'Python' started by Afanasiy, Aug 23, 2003.

  1. Afanasiy

    Afanasiy Guest

    Afanasiy, Aug 23, 2003
    #1
    1. Advertising

  2. Afanasiy

    mackstann Guest

    On Sat, Aug 23, 2003 at 02:03:09AM +0000, Afanasiy wrote:
    > This is fairly simple in PHP, how do I do it in Python?


    In PHP, associative arrays are still regular arrays too, you can access
    them by index (IIRC), and when you loop through them, they maintain the
    order in which you assigned their items. Python seperates associative
    arrays (dicts/hashes) from numerically indexed arrays (lists). You
    can't sort a dict, because a dict has no order. You could do something
    like:

    mydict = { ..whatever.. }

    sortedkeys = mydict.keys()
    sortedkeys.sort()

    for key in sortedkeys:
    print key, mydict[key]

    Then we run into the issue of why we have to do list.sort() in place,
    and I'm sure that's been discussed here a billion times (can't say I've
    been part of any of those discussions though).

    --
    m a c k s t a n n mack @ incise.org http://incise.org
    After a few boring years, socially meaningful rock 'n' roll died out.
    It was replaced by disco, which offers no guidance to any form of life
    more advanced than the lichen family.
    -- Dave Barry, "Kids Today: They Don't Know Dum Diddly Do"
    mackstann, Aug 23, 2003
    #2
    1. Advertising

  3. Afanasiy

    Andrew Dalke Guest

    Afanasiy:
    > This is fairly simple in PHP, how do I do it in Python?
    >
    > http://www.php.net/manual/en/function.ksort.php


    def ksort(d, func = None):
    keys = d.keys()
    keys.sort(func)
    return keys

    for k in ksort(d):
    print k, v

    As a bonus, you don't need to tell the sort to sort numerically
    vs. lexigraphically --- Python's strong typing knows that by
    default. You can pass in an alternate compare function if you
    want.

    And no, I haven't tested it. ;)

    Andrew
    Andrew Dalke, Aug 23, 2003
    #3
  4. Afanasiy

    Afanasiy Guest

    On Sat, 23 Aug 2003 02:34:23 GMT, "Andrew Dalke" <>
    wrote:

    >Afanasiy:
    >> This is fairly simple in PHP, how do I do it in Python?
    >>
    >> http://www.php.net/manual/en/function.ksort.php

    >
    >def ksort(d, func = None):
    > keys = d.keys()
    > keys.sort(func)
    > return keys
    >
    >for k in ksort(d):
    > print k, v
    >
    >As a bonus, you don't need to tell the sort to sort numerically
    >vs. lexigraphically --- Python's strong typing knows that by
    >default. You can pass in an alternate compare function if you
    >want.


    Why wouldn't this be a standard function?
    Afanasiy, Aug 23, 2003
    #4
  5. Afanasiy

    Afanasiy Guest

    Afanasiy, Aug 23, 2003
    #5
  6. Afanasiy

    Andrew Dalke Guest

    Afanasiy:
    > Why wouldn't [ksort] be a standard function?


    Because it isn't needed all that often and can be built (when needed)
    from the underlying primitives very simply. Because if there are a
    lot of similar methods then it becomes harder to remember what each
    one does.

    The normal practice is

    keys = d.keys()
    keys.sort()
    for k in keys:
    ....

    which isn't all that onerous.

    > How about this one?
    >
    > http://www.php.net/manual/en/function.asort.php


    The normal idiom for sorting by value then printing
    the key/value pairs is

    rev_items = [(v, k) for k, v in d.items()]
    rev_items.sort()
    for v, k in rev_items:
    print k, v

    If you want that as a function return just the keys
    in value order

    def asort(d):
    rev_items = [(v, k) for k, v in d.items()]
    rev_items.sort()
    return [k for (v, k) in rev_items]

    As you can see, there are many ways you might want
    to sort a dict. Why should all of them be present in
    the standard dict type when it's really a matter of two
    extra lines to get what you need. Seeing the code in
    this case is much easier than memorizing the 7 different
    sort functions mentioned in the PHP docs.

    Additionally, Python's keys can be more complex than
    a string or int. Eg,

    d = {}
    d[ (0,0) ] = "home"
    d[ (1,3) ] = "school"
    d[ (4,2) ] = "work"

    y_items = [(y, name) for ((x, y), name) in d.items()]
    y_items.sort()
    for y, name in y_items:
    print y, name

    sorts by y position, ignoring x position. PHP doesn't
    have a function for that, but it follows pretty naturally
    from the idiomatically Python way to do it.

    Andrew
    Andrew Dalke, Aug 23, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    416
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    300
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    2,934
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    499
    Chris Rebert
    Jul 29, 2010
  5. Navin
    Replies:
    1
    Views:
    657
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page