[FRPythoneers] sorting a list of dictionaries
chyser at yahoo.com
Mon Jan 20 17:36:50 MST 2003
Here is a snippet which shows a sorting func passed into sort.
yr = int(a[4:]); m = int(a[2:4]); d = int(a[0:2])
return yr * 13 + m * 32 + d
def sf(a, b):
if cvt(a) < cvt(b): return -1
else: return 1
In this example the list was composed of a tuple with a date related ascii name and a dictionary. the func takes the date name and called a second func to convert to a number. the func sf() could just as easily pull a field out of the dict. this example was done using 2.2 so if this doesn't work in 1.5, my apologies.
Andrew Diederich <diederic at boulder.net> wrote:Folks,
I've made a python script to ldap query an exchange server, then kick out
a webpage of phone and email addresses. The result set from the ldap
search is a list of two item tuples. The first item in the tuple is the
DN (distinguished name) and the second is a dictionary of attributes like
uid, rfc822Mailbox, etc.
What I had done to alphabetize the list at first was to just sort the
initial result set, res.sort(). The problem with that is it basically
sorts usernames then, and it's simply not intuitive to see a page of
firstname, lastname sorted that way. It's pretty close, but not quite.
To make the first list just a list of dictionaries I just
exchangelist = 
for x in res: exchangelist.append(x)
But I don't think exchangelist.sort() will sort by, say, cn (common name)
since I didn't tell it to. And I'm not sure how. So what I'd like to do
1) sort the list, then do a "for user in exhangelist: print ......."
2) maybe pull out the values for the cn key into a list, then sort that,
and use that sorted list somehow to re-sort the exchangelist I have.
Does anyone have suggestions at good ways to do this? If I've been
unclear, just let me know, and I can post more of what I've written.
Oh -- This is using python 1.5.2. The linux box I'm running the script
for had ldap already compiled in, or maybe it was the only python module I
could find for ldap. I remember that ldap seemed under-represented in
Thanks in advance,
diederic at boulder.net
This message sent by the FRPythoneers mailing list.
Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the FRPythoneers