[Tutor] Flatten a list in tuples and remove doubles

Peter Otten __peter__ at web.de
Sun Jul 29 22:29:10 CEST 2012


eryksun wrote:

> On Sun, Jul 29, 2012 at 7:21 AM, Peter Otten <__peter__ at 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()...



More information about the Tutor mailing list