Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How about using the groupby function?

a=[(3, 2), (4, 2), (4, 3), (5, 2), (5, 3), (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
from itertools import groupby
[list(g) for k,g in groupby(sorted(a,key=prod),key=prod)]

http://aleph.sagemath.org/?q=0da1a6c6-54aa-4693-9226-23d3fb3f2497&lang=sage

How about using the groupby function?

a=[(3, 2), (4, 2), (4, 3), (5, 2), (5, 3), (5, 4), (6, 2), (6, 3), (6, 4), (6, 5), (98,97)]
from itertools import groupby
print [list(g) for k,g in groupby(sorted(a,key=prod),key=prod)]
print dict([(k,list(g)) for k,g in groupby(sorted(a,key=prod),key=prod)])

http://aleph.sagemath.org/?q=0da1a6c6-54aa-4693-9226-23d3fb3f2497&lang=sagehttp://aleph.sagemath.org/?q=5e10542b-adb9-4c06-8527-98f9b8907469&lang=sage