将列表拆分成随机大小的块的最佳方法是什么?

时间:2014-01-29 18:01:41

标签: python list math

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

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

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

在python中执行此操作的最佳方法是什么?

7 个答案:

答案 0 :(得分:10)

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_chunkmax_chunk之间统一分布块大小。

答案 1 :(得分: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]]

答案 2 :(得分: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)

答案 3 :(得分: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]]

答案 4 :(得分:1)

这是我的方法: 所有结果列表将至少包含一个元素,但它可能会返回包含所有数字的列表。

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

答案 5 :(得分: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]]

答案 6 :(得分:0)

罗比的回答很小:

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]]
相关问题