[Tutor] Sorting list of tuples in two passes
Dayo Adewunmi
contactdayo at gmail.com
Sun Aug 28 18:43:07 CEST 2011
Hi
I have a list of tuples that each have four elements:
[(firstName,lastName,userName,gidNumber),(.....)....]
I'm trying to sort this list in two passes. First by gidNumber and then
the subgroups by lastName. So far i've only been able to sort by
gidNumber. But I can't seem to wrap my mind around lambda, which is what
my browsing around seems to indicate is needed to achieve this?
Thanks
Dayo
import ldap,re
from operator import itemgetter,attrgetter
l = ldap.initialize("ldap://172.20.0.1")
l.simple_bind_s("","")
base_dn = 'ou=People,dc=aust,o=ami-net'
filter = '(objectclass=pilotPerson)'
attrs = ['uid', 'gidNumber', 'sn', 'cn']
users = l.search_s(base_dn, ldap.SCOPE_ONELEVEL, filter, attrs)
def onelist(users):
studentspubline = tuple()
students2xPubLines = []
for aUser in users:
# Get the user details from LDAP
userName = aUser[1]['uid'][0]
gidNumber = aUser[1]['gidNumber'][0]
lastName = aUser[1]['sn'][0]
fullName = aUser[1]['cn'][0]
# Get first names of users
splitFullName = fullName.split()
firstName = splitFullName[1]
if gidNumber[:1] == '9':
studentspubline = userName,lastName,fullName,gidNumber
students2xPubLines.append(studentspubline)
sortedStudents2x = sorted(students2xPubLines, key=itemgetter(3,2))
for userName,lastName,fullName,gidNumber in sortedStudents2x:
print "lastName: %s, gidNumber: %s" %(lastName, gidNumber)
onelist(users)
More information about the Tutor
mailing list