简体   繁体   English

Python - 根据计算的百分比返回列表的区域

[英]Python - return a region of a list based on calculated percentages

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. 我在python中有一个相当长的列表,我希望将它的部分内容返回到基于3个百分比的单个较短列表中。 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. 如果第一个百分比是30%,则需要返回前30%的值,如果第三个百分比是50%,则需要返回我的长列表的后半部分值。

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. 我意识到这根本没有很好的编码,(硬编码长度为169,没有捕捉不同的动作等等)我只是想帮助解释我想要做的事情。

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. 在我的实际实现中,值total,a,b,c将初始化为0,另一种方法将更新它们,因为它们只是计数器。 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. 如果有人能给我任何关于如何更好地做这件事的建议,我将非常感激。

If I understand correctly, you want to keep three lists, based on a certain percentage at the start and end plus the "in between": 如果我理解正确,您希望保留三个列表,基于开头和结尾的特定百分比加上“介于两者之间”:

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):] )

where a and b are the percentages (given from 0 to 100). 其中a和b是百分比(从0到100给出)。 It's nearly the same implementation as yours, but you only need to use two percentages (if the middle list is the remaining part when both ends are trimmed off). 它与您的实现几乎相同,但您只需要使用两个百分比(如果中间列表是两端被修剪掉时的剩余部分)。 I will leave it up to you to incorporate this in your code... 我将把它留在你的代码中...

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. 处理分割百分比多于列表中项目的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM