Sorting a dictionary field belonging to a list

Discussion in 'Python' started by Jocknerd, Sep 17, 2004.

  1. Jocknerd

    Jocknerd Guest

    I have a list called teamlist which contain dictionaries of teams. These
    are the fields in my team dictionary:

    name, won, lost, tied, pf, pa

    To print standings I do the following:

    def printStandings(teamlist):
    for team in teamlist:
    print (team['name'], team['won'], team['lost'], team['tied'],
    team['pf'], team['pa'])

    My question is how would I go about sorting the output based on team['pf']
    for instance?
     
    Jocknerd, Sep 17, 2004
    #1
    1. Advertising

  2. Jocknerd

    Chris Green Guest

    Jocknerd <> writes:

    > My question is how would I go about sorting the output based on team['pf']
    > for instance?


    The simplest way would be to use the sort method on the list in the
    first place. That method takes an optional comparison function that
    you would have to write. See help(lst.sort)

    lst = [ {'name': 'z'}, {'name': 'd'}, {'name': 'e'} ]

    def mycmp(a,b):
    return cmp(a['name'],b['name'])

    lst.sort(mycmp)

    If the original order is important to you, you will be to use
    something like copy.copy() to make a copy of the list.


    --
    Chris Green <>
    "Yeah, but you're taking the universe out of context."
     
    Chris Green, Sep 17, 2004
    #2
    1. Advertising

  3. Jocknerd

    John Lenton Guest

    On Fri, Sep 17, 2004 at 02:51:22PM -0400, Jocknerd wrote:
    > I have a list called teamlist which contain dictionaries of teams. These
    > are the fields in my team dictionary:
    >
    > name, won, lost, tied, pf, pa
    >
    > To print standings I do the following:
    >
    > def printStandings(teamlist):
    > for team in teamlist:
    > print (team['name'], team['won'], team['lost'], team['tied'],
    > team['pf'], team['pa'])
    >
    > My question is how would I go about sorting the output based on team['pf']
    > for instance?


    replace

    for team in teamlist:

    using the decorate-sort-undecorate pattern,

    sortedlist = [(i['pf'], i) for i in teamlist]
    sortedlist.sort()
    for pf, team in sortedlist:

    (this is far faster than calling a python function on each comparison);
    in 2.4 you will be able to do

    for team in sorted(teamlist, operator.itemgetter('pf')):

    --
    John Lenton () -- Random fortune:
    Do not drink coffee in early A.M. It will keep you awake until noon.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.5 (GNU/Linux)

    iD8DBQFBS05hgPqu395ykGsRApb6AJ44oS3lAA6Q3KjqVMTaKAt4+AHMyACgkG9v
    3Unc9K2zG5/vEM/Rs65BnVc=
    =p8Kr
    -----END PGP SIGNATURE-----
     
    John Lenton, Sep 17, 2004
    #3
    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. Jason Heyes
    Replies:
    3
    Views:
    394
    Thomas Matthews
    Mar 22, 2005
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Querying for rows belonging to a month

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Feb 26, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    317
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Feb 27, 2007
  3. Replies:
    2
    Views:
    321
  4. Marc Leconte
    Replies:
    5
    Views:
    281
    Marc Leconte
    Nov 23, 2009
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    170
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page