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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:40:26+00:00 2026-05-28T16:40:26+00:00

testing for exact match of key strings in target strings. output must be tuple

  • 0

testing for exact match of key strings in target strings. output must be tuple of starting points of matches. My code works but i feel like it can be much much neater. How could i return a tuple without converting from an appended list? searched everywhere and can’t seem to find an answer. Thanks!!

from string import *


target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

def subStringMatchExact(target, key):
    match_list = []
    location = 0

    for i in target:
        ans = find(target, key, location)
        if ans >= 0:
            match_list.append(ans)
            location = ans + (len(key))

    print tuple(match_list)

subStringMatchExact(target1, key11)
  • 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-28T16:40:27+00:00Added an answer on May 28, 2026 at 4:40 pm

    This is a perfect job for regular expressions.

    import re
    def subStringMatchExact(target, key):
        regex = re.compile(re.escape(key))
        return tuple(match.start() for match in regex.finditer(target))
    

    Note that this finds non-overlapping matches only. If you want to find overlapping matches, too:

    def subStringMatchExact(target, key):
        regex = re.compile("(?=" + re.escape(key) + ")")
        return tuple(match.start() for match in regex.finditer(target))
    

    Of course, unless you actually need the result to be a tuple, you could just remove the tuple from the last line and have your function return a more efficient generator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about testing for an exact match using regex. car.match(/[ca]+/) returns
Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII
Unit testing is, roughly speaking, testing bits of your code in isolation with test
Unit testing sounds great to me, but I'm not sure I should spend any
I see several other questions about load testing web services. But as far as
I've been searching on this but can't seem to find an exact answer (most
I'm integrating exact code from here: http://labs.makemachine.net/2010/03/simple-android-photo-capture/ How the activity should work: click button
I'm testing OpenGL performance on an Android phone ( HTC Wildfire to be exact
I'm using the exact code that 1and1 suggests for backing up MySQL DBs through
I've been doing a lot of cross-browser testing and I must say that it

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.