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 7651031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:24:15+00:00 2026-05-31T11:24:15+00:00

I have three a string that is the concatenation of three components: one word

  • 0

I have three a string that is the concatenation of three components:

  • one word from list 1 (includes an empty string)
  • one word from list 2
  • one word from list 3 (includes an empty string)

Lists 1, 2 and 3 can be up to 5000 elements. Elements in one list are not in the others (except of the empty string). However, there are words that can be part of other words.

I am looking for an efficient way to find the three components. Right now I am doing the following:

for word in list2:
    if word in long_word:
        try:
           [bef, aft] = long_word.split(word)
        except ValueError: # too many values to unpack
           continue
        if bef in list1 and aft in list3:
           print('Found: {}, {}, {}'.format(bef, word, aft))
           break
else:
    print('Not found')

I wonder if there is a better way. I thought about using the pipe in a regex. But it seems that the number of alternatives are too big as I get: OverflowError: regular expression code size limit exceeded.

Thanks,

Update

I tried a modified version of the proposed solutions:

def fj(long_word, list1, list2, list3):
    for x in filter(long_word.startswith, list1):
        for y in filter(long_word[len(x):].startswith, list2):
            z = long_word[len(x)+len(y):]
            if z in list3:
                yield x, y, z

def sid(long_word, list1, list2, list3):
    for w1 in list1:
        if not long_word.startswith(w1):
            continue
        cut1 = long_word[len(w1):]
        for w2 in list2:
           if not cut1.startswith(w2):
               continue
           cut2 = cut1[len(w2):]
           for w3 in list3:
               if cut2 == w3:
                   yield w1, w2, w3

def my(long_word, list1, list2, list3):
    for word in list2:
        if word in long_word:
            try:
               [bef, aft] = long_word.split(word)
            except ValueError: # too many values to unpack
               continue
            if bef in list1 and aft in list3:
               yield bef, word, aft

This are the (normalized) results for the timing that I get using lists with 8000 elements repeating 10000 times, each time picking randomly one word from each list to generate long_word

  • my: 1.0
  • sid: 4.5
  • fj: 2.7

I am really surprised as I thought that fj’s method was going to be fastest.

  • 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-31T11:24:16+00:00Added an answer on May 31, 2026 at 11:24 am

    Regular expressions probably aren’t a great fit here, I would probably go about it like this:

    for x in filter(long_word.startswith, list1):
        for y in filter(long_word[len(x):].startswith, list2):
            z = long_word[len(x)+len(y):]
            if z in list3:
                print('Found: {}, {}, {}'.format(x, y, z))
                break
        else:
            continue
        break
    else:
        print('Not found')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a string like this: one two three four five six seven
I have two String arrays a,b. String a [] = {one,two,three}; String b []
I have 3 String arrays with constants. eg: String[] digit = {one, two, three};
I have to search a DB2 database for a string that is a concatenation
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a method that effectively takes a string. However, there is a very
I have the following code string three() { return three; } void mutate(string& ref)
I have a property such as IdentityCode string IdentityCode {get;set;} my IdentityCode has three
I have a list of strings in kumo . I am printing three strings.
I have a python template engine that heavily uses regexp. It uses concatenation like:

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.