Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-30 Thread Peter Otten
ranjan das wrote: > I have the following list > > List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', > 'R9'), > ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), > ('G1', > 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] > > > now I want to gro

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-30 Thread Rafael Durán Castañeda
You are right, lambda lis: lis[1:3] would be more readable 2011/3/30 Sander Sweers > On 30 March 2011 00:30, Alan Gauld wrote: > > Wouldn't you just return a tuple of the two elements? > > > > Or am I missing something having jumped into the middle of the thread... > > Ah yes, you are correct.

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Sander Sweers
On 30 March 2011 00:30, Alan Gauld wrote: > Wouldn't you just return a tuple of the two elements? > > Or am I missing something having jumped into the middle of the thread... Ah yes, you are correct. But imo reading "key=lambda l: (l[1], l[0], l[2])" (which would be needed to sort how the OP want

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Alan Gauld
"Sander Sweers" wrote flexible, readable and elegant than using a lamda. How would you sort on the first and last item with lambda? Wouldn't you just return a tuple of the two elements? Or am I missing something having jumped into the middle of the thread... Alan G. __

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Rafael Durán Castañeda
>From python docs: For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2. Example: >>> list_[1][1:3] ('LOOSEFREIGHT', 'MIXEDLCL') >>> As I said i think my approach is more pythonic, but I'm not abso

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Sander Sweers
2011/3/29 Rafael Durán Castañeda : > And more pythonic, I think I don't agree :-). I think itemgetter from the operator module is more flexible, readable and elegant than using a lamda. How would you sort on the first and last item with lambda? Greets Sander __

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Sander Sweers
On 29 March 2011 23:52, Sander Sweers wrote: > On 29 March 2011 22:03, ranjan das wrote: >> New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ), >> ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2',  'LOOSEFREIGHT', 'LCL', 'R4' ), >> ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSE

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Rafael Durán Castañeda
This would work nice too: list_=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] >>> sorted(list_,key=lambda l: l[1:3]) [('G1', 'CFS', 'FC

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Sander Sweers
On 29 March 2011 22:03, ranjan das wrote: > List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), > ('G4', 'CFS', 'FCL', 'R10' ), ('G2',  'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', > 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5')  ] > > > now I want to group this eleme

Re: [Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread Eli Nazarova
On 3/29/2011 4:03 PM, ranjan das wrote: I have the following list List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] now I want

[Tutor] Grouping based on attributes of elements in a List

2011-03-29 Thread ranjan das
I have the following list List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] now I want to group this elements of List first by inde