如何将列表随机分成不同大小的块是最佳方法?

3

我有一个数字列表,例如

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

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

在Python中做这件事的最佳方法是什么?


6
您需要一定数量的子列表吗?它们可以为空吗?子列表的最大长度是多少? - RemcoGerlich
抱歉,我在原帖中没有包含那个信息。睡眠不足是我的借口哈哈。是的,一个固定的数字,它们不能为空,并且最大长度应该在参数中设置。 - Barry
最佳答案对我的目的来说确实很好。谢谢大家的建议。 - Barry
7个回答

17
from itertools import islice
from random import randint

def random_chunk(li, min_chunk=1, max_chunk=3):
    it = iter(li)
    while True:
        nxt = list(islice(it,randint(min_chunk,max_chunk)))
        if nxt:
            yield nxt
        else:
            break

演示:

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

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

这将导致(忽略最后一组)均等分布的块大小在min_chunk和max_chunk之间,包括min_chunk和max_chunk。

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

在Python控制台上进行测试:

>>> 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]]

1
这是我的尝试:

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

一些输出数据:

>>> 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]]

1
你可以简单地遍历列表(X),并使用固定概率(p)将元素放入“最后”子列表,而对于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)

1
这是一种方法:

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]]

0
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]]

0

对 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]]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接