[Brian Beck]>
> py> from itertools import groupby
> py> [''.join(g) for k, g in groupby('  test ing ', lambda x: x.isspace())]
> ['  ', 'test', ' ', 'ing', ' ']

Brilliant solution!

That leads to a better understanding of groupby as a tool for identifying
transitions without consuming them.


> I tried replacing the lambda thing with an attrgetter, but apparently my
> understanding of that isn't perfect... it groups by the identify of the
> bound method instead of calling it...

Right.
attrgetter gets but does not call.

If unicode isn't an issue, then the lambda can be removed:

>>> [''.join(g) for k, g in groupby('  test ing ', str.isspace)]
['  ', 'test', ' ', 'ing', ' ']



Raymond Hettinger


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to