按百分比将列表分区为子列表

时间:2015-09-01 11:29:18

标签: python numpy

我有一个包含值的大型列表。

我想将列表划分为子列表,其大小以百分比形式给出,例如25%,10%,10%,5%,%5,...,%1%(这些应该加起来为100% ),关于大清单的大小。

似乎没有这样的功能。

1 个答案:

答案 0 :(得分:5)

您可以像这样使用np.split

import numpy as np

def splitPerc(l, perc):
    # Turn percentages into values between 0 and 1
    splits = np.cumsum(perc)/100.

    if splits[-1] != 1:
        raise ValueError("percents don't add up to 100")

    # Split doesn't need last percent, it will just take what is left
    splits = splits[:-1]

    # Turn values into indices
    splits *= len(l)

    # Turn double indices into integers.
    # CAUTION: numpy rounds to closest EVEN number when a number is halfway
    # between two integers. So 0.5 will become 0 and 1.5 will become 2!
    # If you want to round up in all those cases, do
    # splits += 0.5 instead of round() before casting to int
    splits = splits.round().astype(np.int)

    return np.split(l, splits)

list = np.arange(100)
percents = np.array([25, 10, 15,  5,  5,  5, 10, 25])
# 100 elements -> lengths of sublists should equal their percents
assert all(percents == map(len, splitPerc(list, percents)))
相关问题