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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:11:00+00:00 2026-05-20T16:11:00+00:00

I have a fairly long list in python which I want to return parts

  • 0

I have a fairly long list in python which I want to return parts of it in a single, shorter list based on 3 percentages. If the first percentage was 30%, it would need to return the first 30% of the values and if the 3rd percentage was 50% it would need to return the last half of the values of my long list.

This is what I have so far, it has issues with rounding and is an ugly solution

  class OM():
    def __init__(self,name):
      self.name = name
      self.total = 120
      self.a = 21
      self.b = 34
      self.c = 65

    def hRange(self,action):
      if self.total > 0:
        a_perc = int(self.a / float(self.total) *169)
        b_perc = int(self.b / float(self.total) *169)
        c_perc = int(self.c / float(self.total) *169)
        if action=='a': return lst[:aperc]
        elif action=='b': return lst[a_perc:a_perc+b_perc]
        elif action=='c': return lst[-c_perc:]

      else:
        raise Exception

I realise this isn’t well coded at all, (hard coded the lst length as 169, doesn’t catch different actions etc etc) I just wanted to help explain what I was trying to do.

In my actual implementation the values total,a,b,c would be initialised at 0 and another method would update them, as they’re just tallys. I just set them as some random values here so the code returns percentages.

I would be hugely appreciative if anyone could give me any advice on how to go about doing this a better way.

  • 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-20T16:11:01+00:00Added an answer on May 20, 2026 at 4:11 pm

    Got carried away with a general solution… I belive this should handle all cases:

    def split_list(mylist, *args):
        ilist = map(lambda p : int(p * len(mylist) / 100.0), args) + [len(mylist)]
        return reduce(lambda l, v : [l[0] + [mylist[l[1]:v]], v], ilist, [[],0])[0]
    

    The function takes a list and a list of percentage split points (in order) and returns you the lists as you need them. Handles cases where there are more percentages of splits than items in the list.

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

Sidebar

Related Questions

I have bunch of strings, some of which are fairly long, like so: movie.titles
I currently have a fairly long LINQ statement which pulls data from two tables:
I have a Windows service which performs a fairly long running task. At the
I have a fairly straightforward Java class here which creates 2 thread pools.... Connects
I have a fairly large C++ application (on Windows, no other platforms planned), which
I'm writing a fairly long running script, which prints progress messages as it goes.
I have a list of links bound to a fairly narrow box; narrow enough
I have a fairly long and complex build automation script. From a configuration management
I have a basic C# console application that executes a fairly long running process
I'm new to CMake. Therefore, I have several questions which I don't want to

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.