2014-01-29 49 views
2

我有一個數字,如[5000,5000,5000,5000,5000,5000]將列表拆分成隨機大小的塊的最佳方法?

我需要創建變成這個列表插入隨機大小的小名單列表的功能列表,如

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

在python中這樣做的最好方法是什麼?

+5

你想要固定數量的子列表嗎?他們可以空着嗎?子列表的最大長度是多少? – RemcoGerlich

+0

對不起,我沒有在原始文章中包含這些信息。沒有足夠的睡眠是我的藉口哈哈。 是的,一個固定的數字,他們不能是空的,最大長度應該在參數中設置 – Barry

+0

最好的答案實際上對我的目的很好。感謝大家的建議。 – Barry

回答

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

這導致(忽略了最後一個塊)均勻分佈塊大小在之間包括和max_chunk

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

哈哈我和你想的一樣... –

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]] 
相關問題