Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:23:36+00:00 2026-05-31T15:23:36+00:00

I wrote a function to perform better than split() built in function (I know

  • 0

I wrote a function to perform better than split() built in function (I know it’s not idiomatic python, but I gave my best), so when I pass this argument:

better_split("After  the flood   ...  all the colors came out."," .")

I’d expected this outcome:

['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']

However, surprisingly, the function causes an incomprehensible (to me) behavior. When it reaches the last two words, it does not suppress the more ” and, rather than add to the outcome list “cam” and “out”, adds to it “came out” and, so, I got this:

['After', 'the', 'flood', 'all', 'the', 'colors', 'came out']

Does someone with more experience understand why this happens?
Thank you in advance for any help!

def better_split(text,markersString):
markers = []
splited = []
for e in markersString:
    markers.append(e)    
for character in text:
    if character in markers:
        point = text.find(character)
        if text[:point] not in character:
            word = text[:point]
            splited.append(word)            
            while text[point] in markers and point+1 < len(text):
                point = point + 1
            text = text[point:]                   
print 'final splited = ', splited

better_split(“This is a test-of the,string separation-code!”, ” ,!-“)

better_split(“After the flood … all the colors came out.”,” .”)

split() WITH MULTIPLE SEPARATIONS
If you are looking for split() with multiple separations, see:
Split Strings with Multiple Delimiters?

The best answer without import re that I found was this:

def my_split(s, seps):
    res = [s]
    for sep in seps:
        s, res = res, []
        for seq in s:
            res += seq.split(sep)
    return res
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:23:37+00:00Added an answer on May 31, 2026 at 3:23 pm

    The point is, the iterator was created and became constant when this line:

    for character in text:
    

    was executed,

    but your aim is to iter the changed text after every for loop.

    So the solution is, move the for loop into a inner function and use it recursively:

    def better_split(text,markersString):
        # simple and better way for 'for e in markerString...'
        markers = list(markersString)
        splited = []
    
        # there is no need to assign variable n, we all know it should be 1
        # n = 1    
    
        def iter_text(text):
            # check if text is an empty string,
            # NOTE this `text` will cover `text` in upper function as to local scope,
            # so it's actually the text everytime iter_text() get,
            # not the one better_split() get.
            if not text:
                return
            # [UPDATES 2012-03-17 01:07 EST]
            # add a flag to judge if there are markers in `text`
            _has_marker = False
            for character in text:
                if character in markers:
                    # set `_has_marker` to True to indicate `text` has been handled
                    _has_marker = True
                    point = text.find(character)
                    word = text[:point]
                    splited.append(word)
                    # check if text[point] is legal, to prevent raising of IndexError
                    while point + 1 <= len(text) and text[point] in markers:
                        point = point + 1
                    text = text[point:]
                    # break the loop when you find a marker
                    # and change `text` according to it,
                    # so that the new loop will get started with changed `text`
                    break
            # if no marker was found in `text`, add the whole `text` to `splited`
            if not _has_marker:
                splited.append(text)
            else:
                iter_text(text)
    
        iter_text(text)
    
        print 'final splited = ', splited
    

    Other details please see the comments in code.

    BTW, may be using builtin function assembly is simpler, although I also think achieve an algorithm independently is a good way to learn language 🙂

    def better_split(s, seprators):
        assert isinstance(seprators, str), 'seprators must be string'
        buf = [s]
        for sep in seprators:
            for loop, text in enumerate(buf):
                buf[loop:loop+1] = [i for i in text.split(sep) if i]
        return buf
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a function in Python which is used to tell me whether the
I wrote a function in Python: def instantiate(c): if inspect.isclass(c): return c() elif isinstance(c,
I know it's not easy to find a master in GINA, but my question
I'm trying to find/write a function that would perform the same operation as imlincomb
I wrote function onclick of a element <div class=left_collapsed>Որոնում</div> <div class=container style=display: none;> <ul>
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote a function in bash script. However, it's complaining about syntax. I really
I wrote this function to get the unread count of google reader items. function
I wrote this function to get a pseudo random float between 0 .. 1
I wrote a function to match fingerprint templates using VC++.NET. Now I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.