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

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]))
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to