Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Sorting a dictionary field belonging to a list

Reply
Thread Tools

Sorting a dictionary field belonging to a list

 
 
Jocknerd
Guest
Posts: n/a
 
      09-17-2004
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?
 
Reply With Quote
 
 
 
 
Chris Green
Guest
Posts: n/a
 
      09-17-2004
Jocknerd <(E-Mail Removed)> 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 <(E-Mail Removed)>
"Yeah, but you're taking the universe out of context."
 
Reply With Quote
 
 
 
 
John Lenton
Guest
Posts: n/a
 
      09-17-2004
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 ((E-Mail Removed)) -- 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)

iD8DBQFBS05hgPqu395ykGsRApb6AJ44oS3lAA6Q3KjqVMTaKA t4+AHMyACgkG9v
3Unc9K2zG5/vEM/Rs65BnVc=
=p8Kr
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem manipulating a list belonging to a class Marc Leconte Python 5 11-23-2009 06:47 PM
Serving files belonging to the web application directory getsanjay.sharma@gmail.com Java 2 09-30-2007 05:28 PM
Querying for rows belonging to a month =?Utf-8?B?bWF2cmlja18xMDE=?= ASP .Net 3 02-27-2007 01:13 AM
Sorting a list of dictionaries by dictionary key Nico Grubert Python 4 05-03-2006 03:19 PM
A program that prompts the user for keywords belonging to C++. Jason Heyes C++ 3 03-22-2005 02:10 PM



Advertisments