Help with splitting
Raymond Hettinger
vze4rx4y at verizon.net
Fri Apr 1 18:17:50 EST 2005
[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
More information about the Python-list
mailing list