[Tutor] Flatten a list in tuples and remove doubles
eryksun
eryksun at gmail.com
Mon Jul 30 01:51:41 CEST 2012
On Sun, Jul 29, 2012 at 4:29 PM, Peter Otten <__peter__ at web.de> wrote:
>
> If you have to sort your data anyway you can sort it first and then apply
> itertools.groupby()...
That was my thinking. I wrote it with groupby (see link below) because
Francesco described field 0 as "in growing order".
http://mail.python.org/pipermail/tutor/2012-July/090607.html
I see now the 2nd call to sorted() in the tuple generator was
redundant. I should have just sorted the data without the key function
to order within each group by field 4 in the first pass:
keyfunc = itemgetter(0,1,2,3)
groups = groupby(sorted(data), keyfunc)
result = []
for group, records in groups:
temp = tuple('%s/%s' % r[5:] for r in records)
result.append(group + temp)
In general though you have to sort and group by the same key.
More information about the Tutor
mailing list