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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:11:35+00:00 2026-05-26T11:11:35+00:00

having a problem here so hopefully could use some help. I have a text

  • 0

having a problem here so hopefully could use some help.

I have a text file with an ID number and a set of “descriptors” on each line. The descriptors may or may not be unique to each line (they can be used multiple times throughout the document).

I basically want to identify all the ID numbers that contain a certain descriptor… my code is working but it only finds the first occurrence of the descriptor, instead of all of them. Any quick fix?

All the descriptors are in a list already.
Example of the text file:

ID_45555 (tab) some irrelevant data (tab) **DESCRIPTOR1** DESCRIPTOR2 DESCRIPTOR3

ID_55555 (tab) some irrelevant data (tab) DESCRIPTOR200 **DESCRIPTOR1** DESCRIPTOR599

Code:

for line in file:
    line = line.strip()
    line = line.split("\t")
    IDNUMBER = line[0]
    DESCRIPTOR = line[2]
    for x in total_list:
        if x in DESCRIPTOR:
            print x, DESCRIPTOR
  • 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-26T11:11:36+00:00Added an answer on May 26, 2026 at 11:11 am

    I’d suggest using a dict for this, with the descriptors as the keys and the corresponding IDs as the values. You go through the file and at each line, add the ID to the list filed in the dictionary under each descriptor. For example:

    by_descriptors = collections.defaultdict(list)
    for line in file:
        id, _, descriptors = line.strip().split("\t")
        for d in descriptors.split():
            by_descriptors[d].append(id)
    # to find all IDs for a given descriptor:
    by_descriptors.get(id, [])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I'm having a bit of a problem here. We have 2 urls let me
I'm having the problem described here: http://groups.google.com/group/microsoft.public.xml.soap/browse_thread/thread/029ee5b5d4fa2440/0895d73c5c3720a1 I am consuming a Web Service using
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
HI Guys, Here I am having problem that How can I post an image
I'm having a problem adding a reference to a .NET assembly I built. Here's
I'm having a little bit of trouble understanding what the problem is here. I
I'm having a problem with keeping a JBoss server running. Here's the command I'm
I'm having a problem with Python's subprocess.Popen method. Here's a test script which demonstrates
I'm having a problem with scala generics. While the first function I defined here

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.