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 group this elements of List  first by index [1] that is (CFS
> and LOOSEFREIGHT ) together and for those elements which are grouped
> together for LOOSEFREIGHT, i want to further divide them into different
> groups based on index[2] that is (LCL or MIXEDLCL)
> 
> 
> So essentially i want them grouped into different lists and my solution
> should be  of the form
> 
> 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')] ]
> 
> How do I do it?
> 
> I managed to do divide them into different lists based on index [1]
> however
> I was not able to further divide them  based on index [2]
> 
> Any help is appreciated

Assuming you have a function 

def group_list(items, key):
# ...

that takes a list and a function to extract the key you can apply that 
recursively first on the initial list, then on all partial lists

def group_recursive(items, keys):
if not keys:
return items
firstkey = keys[0]
rest = keys[1:]
items = group_list(items, firstkey)
if rest:
return [group_recursive(item, rest) for item in items]
return items

if __name__ == "__main__":
from operator import itemgetter
import json

items = [
('G1', 'CFS', 'FCL', 'R1'),
('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'),
('G4', 'CFS', 'FCL', 'R10'),
('G2', 'LOOSEFREIGHT', 'LCL', 'R4'),
('G1', 'CFS', 'FCL', 'R2' ),
('G2', 'LOOSEFREIGHT', 'LCL', 'R5')
]
groups = group_recursive(items, [itemgetter(1), itemgetter(2)])
print json.dumps(groups, indent=2)

One way to implement group_list() is to sort the items and then use 
itertools.groupby() to create the sublists. Another is to put the items into 
a dictionary with key(item) as the dict key and a list of items as the 
value.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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. But imo reading "key=lambda l: (l[1], l[0],
> l[2])" (which would be needed to sort how the OP wanted) hurts my eyes
> ;-). Anyway, both options will work fine for the OP.
>
> Greets
> Sander
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 wanted) hurts my eyes
;-). Anyway, both options will work fine for the OP.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 absolutely sure.
With such approach I don't need importing, I use slicing and you are right,
your approach is more flexible and would work on more cases, but in this
particular case I still think unnecessary. Maybe someone else could tell us
which is the best option.

El 30 de marzo de 2011 00:14, Sander Sweers escribió:

> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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', '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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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', 'FCL', 'R1'), ('G4', 'CFS', 'FCL', 'R10'), ('G1', 'CFS',
'FCL', 'R2'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2', 'LOOSEFREIGHT',
'LCL', 'R5'), ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')]
>>>

And more pythonic, I think

2011/3/29 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 elements of List  first by index [1] that is
> (CFS
> > and LOOSEFREIGHT ) together and for those elements which are grouped
> > together for LOOSEFREIGHT, i want to further divide them into different
> > groups based on index[2] that is (LCL or MIXEDLCL)
> >
> >
> > So essentially i want them grouped into different lists and my solution
> > should be  of the form
> >
> > 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')] ]
> >
> > How do I do it?
>
> You can use itemgetter from the operator module. The below should do
> what you want. I am using sorted to return a new list but you can also
> sort the list in place with list.sort().
>
> >>> import operator
> >>> l =[( '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(l, key=operator.itemgetter(1,2))
> [('G1', 'CFS', 'FCL', 'R1'), ('G4', 'CFS', 'FCL', 'R10'), ('G1',
> 'CFS', 'FCL', 'R2'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2',
> 'LOOSEFREIGHT', 'LCL', 'R5'), ('G3', 'LOOSEFREIGHT', 'MIXEDLCL',
> 'R9')]
>
> Greets
> Sander
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 elements of List  first by index [1] that is (CFS
> and LOOSEFREIGHT ) together and for those elements which are grouped
> together for LOOSEFREIGHT, i want to further divide them into different
> groups based on index[2] that is (LCL or MIXEDLCL)
>
>
> So essentially i want them grouped into different lists and my solution
> should be  of the form
>
> 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')] ]
>
> How do I do it?

You can use itemgetter from the operator module. The below should do
what you want. I am using sorted to return a new list but you can also
sort the list in place with list.sort().

>>> import operator
>>> l =[( '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(l, key=operator.itemgetter(1,2))
[('G1', 'CFS', 'FCL', 'R1'), ('G4', 'CFS', 'FCL', 'R10'), ('G1',
'CFS', 'FCL', 'R2'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2',
'LOOSEFREIGHT', 'LCL', 'R5'), ('G3', 'LOOSEFREIGHT', 'MIXEDLCL',
'R9')]

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 to group this elements of List  first by index [1] that is 
(CFS and LOOSEFREIGHT ) together and for those elements which are 
grouped together for LOOSEFREIGHT, i want to further divide them into 
different groups based on index[2] that is (LCL or MIXEDLCL)



So essentially i want them grouped into different lists and my 
solution should be  of the form


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')] ]


How do I do it?

I managed to do divide them into different lists based on index [1] 
however I was not able to further divide them  based on index [2]


Any help is appreciated



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
You can you list comprehension (three times) checking for membership of 
the relevant items, or you can use for loop to go over all available 
tuples and sort them into different lists using if. In any case, after 
that you create a list of the three required lists.


Eli
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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 index [1] that is (CFS
and LOOSEFREIGHT ) together and for those elements which are grouped
together for LOOSEFREIGHT, i want to further divide them into different
groups based on index[2] that is (LCL or MIXEDLCL)


So essentially i want them grouped into different lists and my solution
should be  of the form

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')] ]

How do I do it?

I managed to do divide them into different lists based on index [1] however
I was not able to further divide them  based on index [2]

Any help is appreciated
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor