473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sorting a dictionary field belonging to a list

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?
Jul 18 '05 #1
2 1352
Jocknerd <jo*******@yahoo.com> 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 <cm*@dok.org>
"Yeah, but you're taking the universe out of context."
Jul 18 '05 #2
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-----

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shalendra chhabra | last post by:
Hi, I just had a tryst with python. I was wondering if python is good enough to do this kind of job -- for it has extensive support of string and pattern matching, ordering and list handling. ...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
7
by: rickle | last post by:
I'm trying to compare sun patch levels on a server to those of what sun is recommending. For those that aren't familiar with sun patch numbering here is a quick run down. A patch number shows...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
4
by: =?Utf-8?B?TUdSaWRlb3V0?= | last post by:
Hello, I have a SQL string that pulls data out of a database - I then calculate completerates based on Hours and # of Completes. I want to sort this data (FieldID and CompleteRates) by CompleteRate...
5
by: robinsiebler | last post by:
I have a data structure that looks like this: # dates = {'2007': {'25': {'06/23/07': {'aerosmith': , # 'Metallica': }, # 'last_song': }}}...
0
GTXY20
by: GTXY20 | last post by:
Hi all, I currently have a CSV file like so 1,a 1,b 1,c 2,a 2,b 2,b
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.