[Python-ideas] Proposal : allowing grouping by relation

Yotam Vaknin tomirendo at gmail.com
Sat Aug 23 21:23:56 CEST 2014


Hi,

I am using groupby (from itertools) to group objects by a key. It would be very useful for me to be able to group objects by the relation of two consecutive  objects or by an object relation to the first object in the current group.

I think it should be done by adding a "relation" keyword to the function, that accept two argument functions that return true or false.

It would also be useful to create a function that enable easily creating relation functions. (Like attrgetter does for keys)
ls = "aaabcdddefgjklm"
groupby(ls, relation=difference(3,key = ord)) 
#[['a', 'a', 'a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'g'], ['j', 'k', 'l', 'm']]

I think in this case the function won't return a key-group tuple, but just a group iterable.

This is already very useful for me, to group event objects in a list if they are close enough in time.

I wrote most of what I had in mind here:
https://github.com/tomirendo/Grouper


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140823/eb7382ae/attachment-0001.html>


More information about the Python-ideas mailing list