bug with itertools.groupby?
Kitlbast
vlad.shevchenko at gmail.com
Tue Oct 6 21:07:40 EDT 2009
On Oct 7, 3:04 am, Raymond Hettinger <pyt... at rcn.com> wrote:
> On Oct 6, 4:06 pm, Kitlbast <vlad.shevche... at gmail.com> wrote:
>
>
>
>
>
> > Hi there,
>
> > the code below on Python 2.5.2:
>
> > from itertools import groupby
>
> > info_list = [
> > {'profile': 'http://somesite.com/profile1', 'account': 61L},
> > {'profile': 'http://somesite.com/profile2', 'account': 64L},
> > {'profile': 'http://somesite.com/profile3', 'account': 61L},
> > ]
>
> > grouped_by_account = groupby(info_list, lambda x: x['account'])
> > for acc, iter_info_items in grouped_by_account:
> > print 'grouped acc: ', acc
>
> > gives output:
>
> > grouped acc: 61
> > grouped acc: 64
> > grouped acc: 61
>
> > am I doing something wrong?
>
> Try another variant of groupby() that doesn't require the data to be
> sorted:
>
> http://code.activestate.com/recipes/259173/
>
> Raymond
I've checked few options of groupby() implementations
1. def groupby(_list, key_func):
res = {}
for i in _list:
k = key_func(i)
if k not in res:
res[k] = [i]
else:
res[k].append(i)
return res
2. def groupby(_list, key_func):
res = {}
[res.setdefault(key_func(i), []).append(i) for i in _list]
return res
second one with setdefault works little bit slower then (1), although
it use list comprehension
More information about the Python-list
mailing list