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

  • SEARCH
  • Home
  • 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 9207829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:28:27+00:00 2026-06-18T00:28:27+00:00

If i have a list of prefix that can be attached to a string,

  • 0

If i have a list of prefix that can be attached to a string, how do i split a string such into it’s prefix and the other characters in the next substring. For example:

prefixes = ['over','under','re','un','co']

str1 = "overachieve"
output: ["over","achieve"]

str2 = "reundo"
output = ["re","un","do"]

Is there a better way to do the above task, maybe with regex or some string functions other than:

str1 = "reundo"
output = []

for x in [p for p in prefixes if p in str1]:
    output.append(x)    
    str1 =  str1.replace(x,"",1)
output.append(str1)
  • 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-06-18T00:28:28+00:00Added an answer on June 18, 2026 at 12:28 am

    Regular expressions are an efficient way to search for many alternative prefixes:

    import re
    
    def split_prefixes(word, prefixes):
        regex = re.compile('|'.join(sorted(prefixes, key=len, reverse=True)))
        result = []
        i = 0
        while True:
            mo = regex.match(word, i)
            if mo is None:
                result.append(word[i:])
                return result
            result.append(mo.group())
            i = mo.end()
    
    
    >>> prefixes = ['over', 'under', 're', 'un', 'co']
    >>> for word in ['overachieve', 'reundo', 'empire', 'coprocessor']:
            print word, '-->', split_prefixes(word, prefixes)
    
    overachieve --> ['over', 'achieve']
    reundo --> ['re', 'un', 'do']
    empire --> ['empire']
    coprocessor --> ['co', 'processor']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unordered list that can look something like this: 1 2.2 1.1.1
I have a method that list listing directories and you can drill in the
i have list of rows that user select and i want to delete them,
I have list of words. I type in a word misspelled. Can I query
I have an optional custom prefix and suffix in my application, that I want
I have a list that I create inside of function1 . I want to
I am currently trying to create a mail list that stores emails into an
Say I have a hash that I can index as: $hash{$document}{$word} From what I
I have a list called L inside a loop that must iterate though millions
If I have a function that can operate on both sets and lists and

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.