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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:49:00+00:00 2026-06-17T23:49:00+00:00

I am new in python and I would like some help for a small

  • 0

I am new in python and I would like some help for a small problem. I have a file whose each line has an ID plus an associated number. More than one numbers can be associated to the same ID. How is it possible to get only the ID plus the largest number associated with it in python?

Example:

Input: ID_file.txt

ENSG00000133246 2013
ENSG00000133246 540
ENSG00000133246 2010
ENSG00000253626 465
ENSG00000211829 464
ENSG00000158458 2577
ENSG00000158458 2553

What I want is the following:

ENSG00000133246 2013
ENSG00000253626 465
ENSG00000211829 464
ENSG00000158458 2577

Thanks in advance for any help!

  • 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-17T23:49:01+00:00Added an answer on June 17, 2026 at 11:49 pm

    I would think there are many ways to do this I would though use a dictionary

    from collections import defaultdict
    
    id_value_dict = defaultdict()
    for line in open(idfile.txt).readlines():
        id, value = line.strip().split()
        if id not in id_value_dict:
            id_value_dict[id] = int(value)
        else:
            if id_value_dict[id] < int(value):
                id_value_dict[id] = int(value)
    

    Next step is to get the dictionary written out

    out_ref = open(outputfile.txt,'w')
    for key, value in id_value_dict:
        outref.write(key + '\t' + str(value)
    
    outref.close()
    

    There are slicker ways to do this, I think the dictionary could be written in a one-liner using a lamda or a list-comprehension but I like to start simple

    Just in case you need the results sorted there are lots of ways to do it but I think it is critical to understand working with lists and dictionaries in python as I have found that the learning to think about the right data container is usually the key to solving many of my problems but I am still a new. Any way if you need the sorted results a straightforward was is to

     id_value_dict.keys().sort() 
    

    SO this is one of the slick things about python id_value__dict.keys() is a list of the keys of the dictionary sorted

     out_ref = open(outputfile.txt,'w')
     for key in id_value_dict.keys():
         outref.write(key + '\t' + str(id_value_dict[key])
    
     outref.close()
    

    its really tricky because you might want (I know I always want) to code

      my_sorted_list = id_value_dict.keys().sort()
    

    However you will find that my_sorted_list does not exist (NoneType)

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

Sidebar

Related Questions

I'm pretty new to Python programming and would appreciate some help to a problem
I'm new to python and would like some assistance. I have a variable q
I'm new to python and I would like some help. I created some classes
I would need some help in python. I'm new in python. I used to
I'm new to Python and would like to know of some good framework /
I'm new to OpenCV and would like to use its Python binding. When trying
I'm new to programming, I have some basic python programming from college, I am
I am new to Django. I would like to run some command from some
I would like to have an interface between Python and sqlite. Both are installed
I am new to python and would like to be pointed in the next

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.