Python 将列表拆分为随机大小的块的最佳方法?

Python 将列表拆分为随机大小的块的最佳方法?,python,list,math,Python,List,Math,我有一个数字列表,比如[5000,5000,5000,5000,5000] 我需要创建一个函数,将该列表转换为随机大小的较小列表的列表,例如 [5000,5000],[5000,5000,5000],[5000]] 在python中实现这一点的最佳方法是什么?以下是我的尝试: from random import randint def random_list_split(data): split_list = [] L = len(data) i = 0 wh

我有一个数字列表,比如[5000,5000,5000,5000,5000]

我需要创建一个函数,将该列表转换为随机大小的较小列表的列表,例如

[5000,5000],[5000,5000,5000],[5000]]

在python中实现这一点的最佳方法是什么?

以下是我的尝试:

from random import randint

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


这导致块大小在
min\u chunk
max\u chunk
之间(忽略最后一个块)均匀分布。

您可以简单地遍历列表(
X
),并以固定的概率(
p
)将元素放入“last”子列表中,并使用
1-p
添加到新的子列表中

import random

sublists = []
current = []
for x in X:
    if len(current)>0 and random.random() >= p:
        sublists.append(current)
        current = []
    current.append(x)
sublists.append(current)
这里有一个方法:

def randsplit(lst):
    out = [[]]
    for item in lst:
        out[-1].append(item)
        if random.choice((True, False)):
            out.append([])
    return [l for l in out if len(l)]
此方法既不会变异
lst
,也不会返回任何空列表。样本:

>>> 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]]
这是我的方法: 所有结果列表将至少有一个元素,但它可能返回一个包含所有数字的列表

import random

def randomSublists(someList):
    resultList = [] #result container
    index = 0 #start at the start of the list
    length = len(someList) #and cache the length for performance on large lists
    while (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 it
        index = index + randomNumber #increment index by amount of list used
    return resultList #return the list of randomized sublists

roippi答案的微小变化:

In [1]: import itertools

In [2]: import random

In [3]: def random_chunk(li, min_chunk=1, max_chunk=3):
   ...:     it = iter(li)
   ...:     return list(
   ...:         itertools.takewhile(
   ...:             lambda item: item,
   ...:             (list(itertools.islice(it, random.randint(min_chunk, max_chunk)))
   ...:              for _ in itertools.repeat(None))))
   ...: 

In [4]: random_chunk(range(10), 2, 4)
Out[4]: [[0, 1], [2, 3, 4], [5, 6, 7], [8, 9]]

In [5]: random_chunk(range(10), 2, 4)
Out[5]: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]

In [6]: random_chunk(range(10), 2, 4)
Out[6]: [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [7]: random_chunk(range(10), 2, 2)
Out[7]: [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

In [8]: random_chunk(range(10), 1, 2)
Out[8]: [[0, 1], [2, 3], [4], [5], [6], [7, 8], [9]]

In [9]: random_chunk(range(10), 1, 2)
Out[9]: [[0, 1], [2, 3], [4], [5], [6], [7], [8], [9]]

In [10]: random_chunk(range(10), 1, 20)
Out[10]: [[0], [1, 2, 3], [4, 5, 6, 7, 8, 9]]

In [11]: random_chunk(range(10), 1, 20)
Out[11]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [12]: random_chunk(range(10), 1, 20)
Out[12]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [13]: random_chunk(range(10), 1, 20)
Out[13]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [14]: random_chunk(range(10), 1, 20)
Out[14]: [[0], [1, 2, 3, 4, 5, 6, 7, 8], [9]]

您想要固定数量的子列表吗?它们可以是空的吗?子列表的最大长度是多少?很抱歉,我没有在原始帖子中包含这些信息。睡眠不足是我的借口哈哈。是的,一个固定的数字,它们不能是空的,最大长度应该在论据中设置。从我的目的来看,最上面的答案实际上很好。谢谢大家的建议。
>>> 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]]
import random

def randomSublists(someList):
    resultList = [] #result container
    index = 0 #start at the start of the list
    length = len(someList) #and cache the length for performance on large lists
    while (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 it
        index = index + randomNumber #increment index by amount of list used
    return resultList #return the list of randomized sublists
>>> 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]]
import random

old_list = [5000, 5000, 5000, 5000, 5000, 5000]
new_list = []
def random_list(old, new):
    temp = []
    for each_item in old:
        temp.append(each_item)
        chance = random.randint(0,1)
        if chance < 1:
            new.append(temp)
            temp = []
    return new
[[5000, 5000, 5000, 5000], [5000, 5000]]
[[5000, 5000, 5000, 5000], [5000], [5000]]
[[5000], [5000], [5000, 5000], [5000, 5000]]
In [1]: import itertools

In [2]: import random

In [3]: def random_chunk(li, min_chunk=1, max_chunk=3):
   ...:     it = iter(li)
   ...:     return list(
   ...:         itertools.takewhile(
   ...:             lambda item: item,
   ...:             (list(itertools.islice(it, random.randint(min_chunk, max_chunk)))
   ...:              for _ in itertools.repeat(None))))
   ...: 

In [4]: random_chunk(range(10), 2, 4)
Out[4]: [[0, 1], [2, 3, 4], [5, 6, 7], [8, 9]]

In [5]: random_chunk(range(10), 2, 4)
Out[5]: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]

In [6]: random_chunk(range(10), 2, 4)
Out[6]: [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [7]: random_chunk(range(10), 2, 2)
Out[7]: [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

In [8]: random_chunk(range(10), 1, 2)
Out[8]: [[0, 1], [2, 3], [4], [5], [6], [7, 8], [9]]

In [9]: random_chunk(range(10), 1, 2)
Out[9]: [[0, 1], [2, 3], [4], [5], [6], [7], [8], [9]]

In [10]: random_chunk(range(10), 1, 20)
Out[10]: [[0], [1, 2, 3], [4, 5, 6, 7, 8, 9]]

In [11]: random_chunk(range(10), 1, 20)
Out[11]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [12]: random_chunk(range(10), 1, 20)
Out[12]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [13]: random_chunk(range(10), 1, 20)
Out[13]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

In [14]: random_chunk(range(10), 1, 20)
Out[14]: [[0], [1, 2, 3, 4, 5, 6, 7, 8], [9]]