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.
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.
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.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With