eryksun wrote: > On Sun, Jul 29, 2012 at 7:21 AM, Peter Otten <__pete...@web.de> wrote: >> >> If you don't have to deal with large datasets many of its functions can >> easily be emulated with lists and loops though. As an example here's the >> grouping with a plain vanilla dict: >> >> groups = {} >> for item in data: >> groups.setdefault(item[:4], []).append(item[-2:]) >> >> result = [key + tuple("{}/{}".format(*v) for v in values) for key, values >> in groups.items()] > > Or use a defaultdict: > > from collections import defaultdict
Yes, I would have used that, but I wanted to show that there was a reasonable solution that did not rely on any libraries. > groups = defaultdict(list) > for item in data: > groups[item[:4]].append(item[4:]) > > result = [] > for key in sorted(groups): > groups[key].sort() > result.append(key + tuple('%s/%s' % v[1:] for v in groups[key])) If you have to sort your data anyway you can sort it first and then apply itertools.groupby()... _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor