Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partition a list into sublists by percentage [duplicate]

Tags:

python

numpy

I have a large list containing values.

I would like to partition the list into sublists whose size is given in percentage, like 25%, 10%, 10%, 5%, %5, ..., %1% (these should add up to 100%), with respect to the size of the big list.

It seems there is no function like this.

like image 809
NinjaGaiden Avatar asked Sep 01 '15 11:09

NinjaGaiden


People also ask

How do I split a list into a sub list?

Split List Into Sublists Using the array_split() Function in NumPy. The array_split() method in the NumPy library can also split a large array into multiple small arrays. This function takes the original array and the number of chunks we need to split the array into and returns the split chunks.

How do you split a list in Python 3?

Python 3 - String split() Method The split() method returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.


1 Answers

You can use np.split like this:

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)))
like image 191
swenzel Avatar answered Oct 11 '22 10:10

swenzel