On 07/28/2012 07:12 PM, Francesco Loffredo wrote: > Il 28/07/2012 20:41, eryksun wrote: >> 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')] >> > Hmmmm... it happened again. I spend some time and effort to solve a > problem, I feel like I'm almost a Real Python Programmer... and > someone spoils my pride showing me some standard module whose name I > barely remember, that can solve that same problem in a few lines... > > Hey! That's a GREAT solution, Eryksun! Nothing wrong with you, really! > > Every time this happens, I have to admit that I'm a newbie and I've > still got a lot to learn about Python. Especially about its wonderful > standard library. Better than Apple's App Store: for anything you can > think, there's a Module. Problem is, I still can't readily recall > which to use for a given problem. > My bad, now I'll RTFM again and I will study very carefully the > operator and itertools modules. Who knows, maybe in a few decades I'll > be able to say "This looks like a good problem for Module X" too. > > Francesco >
You might find it enlightening to look up: http://www.doughellmann.com/PyMOTW/ which explores the Pythons standard library. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor