On Sat, Jul 28, 2012 at 11:12 AM, Francesco Loffredo <f...@libero.it> wrote: > > I had to study carefully your present and desired lists, and I understood > what follows (please, next time explain !): > - each 7-tuple in your present list is a record for some measure relative to > a person. Its fields are as follows: > - field 0: code (I think you want that in growing order) > - field 1: group code (could be a class or a group to which both of your > example persons belong) > - fields 2, 3: surname and name of the person > - field 4: progressive number of the measure (these are in order > already, but I think you want to enforce this) that you want to exclude from > the output list while keeping the order > - field 5, 6: numerator and denominator of a ratio that is the measure. > you want the ratio to be written as a single string: "%s/%s" % field5, > field6
This looks like a good problem for itertools.groupby. My solution below needs error checking and testing, but this is where I'd start: data = [ (0, '3eA', 'Dupont', 'Juliette', 0, 11.0, 10.0), (0, '3eA', 'Dupont', 'Juliette', 1, 4.0, 5.0), (0, '3eA', 'Dupont', 'Juliette', 2, 17.5, 30.0), (0, '3eA', 'Dupont', 'Juliette', 3, 3.0, 5.0), (0, '3eA', 'Dupont', 'Juliette', 4, 4.5, 10.0), (0, '3eA', 'Dupont', 'Juliette', 5, 35.5, 60.0), (1, '3eA', 'Pop', 'Iggy', 0, 12.0, 10.0), (1, '3eA', 'Pop', 'Iggy', 1, 3.5, 5.0), (1, '3eA', 'Pop', 'Iggy', 2, 11.5, 30.0), (1, '3eA', 'Pop', 'Iggy', 3, 4.0, 5.0), (1, '3eA', 'Pop', 'Iggy', 4, 5.5, 10.0), (1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0), ] from operator import itemgetter from itertools import groupby #first sort by keyfunc, then group by it keyfunc = itemgetter(0,1,2,3) groups = groupby(sorted(data, key=keyfunc), keyfunc) result = [] for group, records in groups: temp = tuple('%s/%s' % r[5:] for r in sorted(records, key=itemgetter(4))) result.append(group + temp) >>> result [(0, '3eA', 'Dupont', 'Juliette', '11.0/10.0', '4.0/5.0', '17.5/30.0', '3.0/5.0', '4.5/10.0', '35.5/60.0'), (1, '3eA', 'Pop', 'Iggy', '12.0/10.0', '3.5/5.0', '11.5/30.0', '4.0/5.0', '5.5/10.0', '40.5/60.0')] _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor