Skip to content Skip to sidebar Skip to footer

Best Way To Split A List Into Randomly Sized Chunks?

I have a list of numbers such as [5000, 5000, 5000, 5000, 5000, 5000] I need to create a function that turns that list into a list of randomly sized smaller lists, such as [[5000,

Solution 1:

from itertools import islice
from random import randint

defrandom_chunk(li, min_chunk=1, max_chunk=3):
    it = iter(li)
    whileTrue:
        nxt = list(islice(it,randint(min_chunk,max_chunk)))
        if nxt:
            yield nxt
        else:
            break

demo:

li= [5000, 5000, 5000, 5000, 5000, 5000]

list(random_chunk(li))Out[45]: [[5000, 5000, 5000], [5000], [5000, 5000]]

This results in a (ignoring the last chunk) uniform distribution of chunk sizes between min_chunk and max_chunk, inclusively.

Solution 2:

This is my approach to it: All resultant lists will have at least one element, but it may return a list with all numbers.

import random

def randomSublists(someList):
    resultList = [] #result containerindex = 0#start at the start of the listlength = len(someList) #and cache the length for performance on large listswhile (index < length):
        randomNumber = random.randint(1, length-index+1) #get a number between 1 and the remaining choices
        resultList.append(someList[index:index+randomNumber]) #append a list starting at index with randomNumber length to itindex = index + randomNumber #increment index by amount of list usedreturn resultList #return the list of randomized sublists

Testing on the Python console:

>>> randomSublist([1,2,3,4,5])
[[1], [2, 3, 4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1], [2, 3], [4], [5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2, 3, 4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2], [3], [4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2, 3, 4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2, 3, 4], [5]]
>>> randomSublist([1,2,3,4,5])
[[1], [2, 3, 4], [5]]
>>> randomSublist([1,2,3,4,5])
[[1], [2, 3], [4], [5]]
>>> randomSublist([1,2,3,4,5])
[[1], [2], [3, 4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2, 3, 4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2, 3], [4, 5]]
>>> randomSublist([1,2,3,4,5])
[[1, 2, 3, 4], [5]]

Solution 3:

You can simply iterate through the list (X) and with fixed probability (p) put element in the "last" sublist and with 1-p to the new one

import random

sublists = []
current = []
for x in X:
    iflen(current)>0 and random.random() >= p:
        sublists.append(current)
        current = []
    current.append(x)
sublists.append(current)

Solution 4:

Here's one method:

def randsplit(lst):
    out = [[]]for item in lst:
        out[-1].append(item)
        ifrandom.choice((True, False)):
            out.append([])
    return [l for l in out iflen(l)]

This method neither mutates lst nor returns any empty lists. A sample:

>>>l=  [5000, 5000, 5000, 5000, 5000, 5000]
>>>randsplit(l) 
[[5000, 5000], [5000, 5000], [5000, 5000]]
>>>randsplit(l) 
[[5000, 5000, 5000], [5000, 5000], [5000]]
>>>randsplit(l) 
[[5000], [5000], [5000, 5000], [5000], [5000]]

Solution 5:

Heres my attempt:

from random import randint

def random_list_split(data):
    split_list = []
    L = len(data)
    i = 0while i < L:
        r = randint(1,L-i)
        split_list.append(data[i:i+r])
        i = i + r
    return split_list

Some output data:

>>>random_list_split(test)
[[5000, 5000, 5000, 5000, 5000, 5000], [5000], [5000], [5000]]
>>>random_list_split(test)
[[5000, 5000, 5000, 5000], [5000, 5000], [5000, 5000], [5000]]
>>>random_list_split(test)
[[5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000], [5000]]
>>>random_list_split(test)
[[5000, 5000], [5000, 5000, 5000, 5000], [5000], [5000], [5000]]
>>>random_list_split(test)
[[5000, 5000, 5000, 5000, 5000, 5000], [5000], [5000], [5000]]
>>>random_list_split(test)
[[5000, 5000, 5000, 5000, 5000, 5000], [5000], [5000], [5000]]
>>>random_list_split(test)
[[5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000]]

Post a Comment for "Best Way To Split A List Into Randomly Sized Chunks?"