[Python-ideas] Optional keepsep argument in str.split()
Stephen J. Turnbull
stephen at xemacs.org
Thu Aug 29 17:36:21 CEST 2013
Rob Cliffe writes:
>
> On 28/08/2013 20:40, Marco Buttu wrote:
>
> > It should be attached to the previous part, exactly as my example
> >
> If there is a leading separator in your original string, you will have
> to decide whether to keep it prefixed to the first element of your split
> list.
No, he wants it affixed to the null first element, not prefixed to the
first non-null element. That's not a problem with his proposal.
The problem with his proposal is that it's quite incoherent. The
semantics of 'separator' is precisely that it doesn't belong to the
preceding element nor to the following element, but rather is an
emergent property of the juxtaposition of *two* items (either of which
might be null!) The C semicolon that he uses as an example is
syntactically not a separator, it's a terminator. That's precisely
why he wants it affixed!
Also, his "use case" isn't really one. "Nobody" really wants "a;b;c;"
to become ["a;", "b;", "c;"] (consider s/a/if var/), and they
"certainly" don't want "a; b; c;" to become ["a;", " b;", " c;"].
Finally, if you *do* for some reason (despite the absolute confidence
that I know better than you that I display above, I'm probably wrong
:-), re.find_all("[^;]*;", "a;b;c;") does exactly what you want.
-1 on keepsep in str.split().
Steve
More information about the Python-ideas
mailing list