[Tutor] Grouping based on attributes of elements in a List
Sander Sweers
sander.sweers at gmail.com
Wed Mar 30 00:08:38 CEST 2011
On 29 March 2011 23:52, Sander Sweers <sander.sweers at gmail.com> wrote:
> On 29 March 2011 22:03, ranjan das <ranjand2005 at gmail.com> wrote:
>> New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ),
>> ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ),
>> ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSEFREIGHT', 'MIXEDLCL',
>> 'R9')] ]
Hmm, looking at your New_list you actually want to sort on 3
"indexes", on 1 then 0 then 2. So change the key= part from before to
key=operator.itemgetter(1,0,2) and it will match your New_list
perfectly.
Greets
Sander
More information about the Tutor
mailing list