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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:07:36+00:00 2026-05-15T06:07:36+00:00

I am having trouble picking the best data structure for solving a problem. The

  • 0

I am having trouble picking the best data structure for solving a problem.

The problem is as below:

  1. I have a nested list of identity codes where the sublists are of varying length.

    li = [['abc', 'ghi', 'lmn'], ['kop'], ['hgi', 'ghy']]
    
  2. I have a file with two entries on each line; an identity code and a number.

    abc      2.93  
    ghi      3.87  
    lmn      5.96  
    

Each sublist represents a cluster. I wish to select the i.d. from each sublist with the highest number associated with it, append that i.d. to a new list and ultimately write it to a new file.

What data structure should the file with numbers be read in as?

Also, how would you iterate over said data structure to return the i.d. with the highest number that matches the i.d. within a sublist?

Thanks,
S 🙂

  • 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-15T06:07:37+00:00Added an answer on May 15, 2026 at 6:07 am

    You can read the file into a dictionary (string=>int), then use a list comprehension to get the highest identity code from each sublist.

    d = {}
    with open("data", 'rb') as data:
      for line in data:
        key, val = line.split(' ')
        d[key] = float(val)
    
    ids = [max(sublist, key=lambda k: d[k]) for sublist in li]
    

    For Python 2.4, use:

    ids = []
    for sublist in li:
      subnums = map(lambda x: d[x], sublist)
      ids.append(sublist[subnums.index(max(subnums))])
    

    As noted, this is O(n).

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

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is not an API and it is not part… May 16, 2026 at 5:26 pm
  • Editorial Team
    Editorial Team added an answer This will result in hovering over any .body element showing… May 16, 2026 at 5:26 pm
  • Editorial Team
    Editorial Team added an answer It’s easy! Just use a regular expression! var m =… May 16, 2026 at 5:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm looking at stress testing our website and having trouble picking the right tool.
I'm trying to implement picking using Pyglet's OpenGL wrapper, but I'm having trouble converting
Having trouble working out how to do the following - I want to list
I having trouble getting append() to work in safari for some reason, but it
I'm having trouble debugging a C++ program in Eclipse (the latest RC of Helios,
Since upgrading from mvc 2 beta 2 to rc I'm having trouble with an
[I solved this myself, see my answer for cause] I'm having trouble getting form
Proficient with SQL but new to MDX, I am having trouble getting my head
Having trouble with an HQL query. If I remove the avg(..) from it it
Im having trouble getting my head round subqueries in Mysql. Fairly simple ones are

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.