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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:38:50+00:00 2026-06-17T20:38:50+00:00

I am trying to write a python code to match things from two lists

  • 0

I am trying to write a python code to match things from two lists in python.

One tab-delimited file looks like this:

COPB2

KLMND7

BLCA8

while the other file2 has a long list of similar looking “names”, if you will. There should be some identical matches in the file, which I have succeeded in identifying and writing out to a new file. The problem is when there are additional characters at the end of one of the “names”. For example, COPB2 from above should match COPB2A in file2, but it does not. Similarly KLMND7 should match KLMND79. Should I use regular expressions? Make them into strings? Any ideas are helpful, thank you!

What I have worked on so far, after the first response seen below:

with open(in_file1, "r") as names:
for line in names:
    file1_list = [i.strip() for i in line.split()]
    file1_str = str(file1_list)

with open(in_file2, "r") as symbols:
for line in symbols:
    items = line.split("\t")
    items = str(items)
    matches = items.startswith(file1_str)
    print matches

This code returns False when I know there should be some matches.

  • 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-17T20:38:51+00:00Added an answer on June 17, 2026 at 8:38 pm

    string.startswith() No need for regex, if it’s only trailing characters

    >>> g = "COPB2A"
    >>> f = "COPB2"
    >>> g.startswith(f)
    True
    

    Here is a working piece of code:

    file1_list = []
    with open(in_file1, "r") as names:
        for line in names:
            line_items = line.split()
            for item in line_items:
                file1_list.append(item)
    
    matches = []
    with open(in_file2, "r") as symbols:
        for line in symbols:
            file2_items = line.split()
            for file2_item in file2_items:
                for file1_item in file1_list:
                    if file2_item.startswith(file1_item):
                        matches.append(file2_item)
                        print file2_item
    print matches
    

    It may be quite slow for large files. If it’s unacceptable, I could try to think about how to optimize it.

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

Sidebar

Related Questions

Trying to write a python script to extract lines from a file. The file
I'm trying to write metadata to a pdf file using the following python code:
I'm trying to write some Python code that will establish an invisible relay between
I'm trying to write a python regular expression that will match both these URLs:
I am trying to write a python script that takes record data like this
i have been trying to write a program in python to read two text
I am trying to write some code that will function like google calendars quick
I am trying to write a Python code analyzer, and I am trying to
I'm trying to write a python script that does the following from within a
I'm trying to write a Python code that will import LANDSAT satellite images into

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.