Python - returns list area based on calculated percent

I have a rather long list in python that I want to return parts of it in one shorter list based on 3 percent. If the first percentage was 30%, he would need to return the first 30% of the values, and if the 3rd percentage was 50%, he would need to return the last half of the values ​​in my long list.

This is what I still have, it has problems 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 understand that this is not encoded at all (lst length is hardcoded as 169, does not capture different actions, etc.). I just wanted to help explain what I was trying to do.

total, a, b, c 0, , . , .

, - - , .

+3
2

... , :

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]

( ) . , , .

+1

, " ":

def split_list(mylist, a, b):
    l = len(mylist)
    return ( mylist[:int(a*l/100)], 
             mylist[int(a*l/100):-int(b*l/100)],
             mylist[-int(b*l/100):] )

a b - ( 0 100). , , ( - , ). , ...

+2

All Articles