On Mon, Jul 2, 2018 at 2:52 AM Michael Selik <m...@selik.org> wrote: > On Mon, Jul 2, 2018 at 2:32 AM Nicolas Rolin <nicolas.ro...@tiime.fr> > wrote: > >> I think the current default quite weird, as it pretty much account to a >> count() of each key (which can be useful, but not really what I except from >> a grouping). I would prefer a default that might return an error to a >> default that says ok and output something that is not what I might want. >> For example the default could be such that grouping unpack tuples (key, >> value) from the iterator and do what's expected with it (group value by >> key). It is quite reasonable, and you have one example with (key, value) in >> your example, and no example with the current default. It also allows to >> use syntax of the kind >> >> >grouping((food_type, food_name for food_type, food_name in foods)) >> >> which is pretty nice to have. >> > > I'm of two minds on this point. First, I agree that it'd be nice to handle > the (key, value) pair case more elegantly. It comes to mind often when > writing examples, even if proportionally less in practice. > > Second, I'll paraphrase "Jakob's Law of the Internet User Experience" -- > users spend most of their time using *other* functions. Because > itertools.groupby and other functions in Python established a standard for > the behavior of key-functions, I want to keep that standard. > > Third, some classes might have a rich equality method that allows many > interesting values to all wind up in the same group even if using the > default "identity" key-function. > > Thanks for the suggestion. I'll include it in the PEP, at least for > documenting all reasonable options. >
It might not be pure (does not default to identity key-function), but it sure seems practical. :: from itertools import groupby as _groupby from operator import itemgetter def grouping(iterable, key=itemgetter(0)): ''' Group elements of an iterable into a dict of lists. The ``key`` is a function computing a key value for each element. Each key corresponds to a group -- a list of elements in the same order as encountered. By default, the key-function gets the 0th index of each element. .>>> grouping(['apple', 'banana', 'aardvark']) {'a': ['apple', 'aardvark'], 'b': ['banana']} ''' groups = {} for k, g in _groupby(iterable, key): groups.setdefault(k, []).extend(g) return groups
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/