split

Terry Reedy tjreedy at udel.edu
Fri Aug 9 01:06:41 EDT 2002


"Seo Sanghyeon" <unendliche at hanmail.net> wrote in message
news:45e6545c.0208082006.71908e01 at posting.google.com...
> How can I split a sequence other than a string?
>
> >>> split([1,0,1,1,0,0,1,0], lambda x: x == 0)
> [[1], [1, 1], [1]]
>
> ----
> split=lambda q,p=lambda x:x==' ':[q[s+1:e] for s,e in zip(*[h+m+t
for h,t in
> [([-1],[]),([],[l for l in [len(q)]])] for m in [[i for i in
range(l) if p(q
> [i])]]]) if s+1!=e]

Another advertisement for the benefit of breaking long, complicated
expressions into multiple lines and using temp variables ;-)

If you want to exactly imitate string.split, the result needs two
empty lists:
[[1], [1,1], [], [1], []]

>>> '10110010'.split('0')
['1', '11', '', '1', '']

Terry J. Reedy







More information about the Python-list mailing list