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, , . , .
, - - , .