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 (jo**@grulic.org.ar) -- 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-----