[Tutor] Sorting list of tuples in two passes

Dayo Adewunmi contactdayo at gmail.com
Sun Aug 28 18:43:07 CEST 2011


I have a list of tuples that each have four elements:


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?



import ldap,re
from operator import itemgetter,attrgetter

l = ldap.initialize("ldap://")

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

         sortedStudents2x = sorted(students2xPubLines, key=itemgetter(3,2))

     for userName,lastName,fullName,gidNumber in sortedStudents2x:
         print "lastName: %s, gidNumber: %s" %(lastName, gidNumber)


More information about the Tutor mailing list