[Tutor] python 3.3 split method confusion

Christian Alexander christian.h.alexander at gmail.com
Tue Jan 7 23:30:29 CET 2014


That makes total sense now.  I was  just curious as to why it didn't output
the arbitrary delimiter in the list, or if there was a specific reason for
it.


On Sat, Jan 4, 2014 at 10:03 PM, Danny Yoo <dyoo at hashcollision.org> wrote:

> One of the common cases for split() is to break a line into a list of
> words, for example.
>
> #####################################
> >>> 'hello this is a test'.split()
> ['hello', 'this', 'is', 'a', 'test']
> #####################################
>
> The Standard Library can not do everything that we can conceive of as
> being useful, because that set is fairly large.
>
> If the Standard Library doesn't do it, we'll probably need to do it
> ourselves, or find someone who has done it already.
>
>
> ##########################################
> >>> def mysplit(s, delim):
> ...     start = 0
> ...     while True:
> ...         index = s.find(delim, start)
> ...         if index != -1:
> ...             yield s[start:index]
> ...             yield delim
> ...             start = index + len(delim)
> ...         else:
> ...             yield s[start:]
> ...             return
> ...
> >>> list(mysplit("this,is,a,test", ","))
> ['this', ',', 'is', ',', 'a', ',', 'test']
> ##########################################
>



-- 
Regards,

Christian Alexander
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140107/885d5c7f/attachment.html>


More information about the Tutor mailing list