I는 같은목록을 임의의 크기의 청크로 분할하는 가장 좋은 방법은 무엇입니까?
등의 [5000, 5000, 5000, 5000, 5000, 5000]I 임의로 크기가 작은리스트리스트에리스트를 전환하는 기능을 만들기 위해 필요한만큼 번호 목록이
[5000, 5000], [5000, 5000, 5000], [5000]
파이썬에서이 작업을 수행하는 가장 좋은 방법은 무엇입니까? Heres는
I는 같은목록을 임의의 크기의 청크로 분할하는 가장 좋은 방법은 무엇입니까?
등의 [5000, 5000, 5000, 5000, 5000, 5000]I 임의로 크기가 작은리스트리스트에리스트를 전환하는 기능을 만들기 위해 필요한만큼 번호 목록이
[5000, 5000], [5000, 5000, 5000], [5000]
파이썬에서이 작업을 수행하는 가장 좋은 방법은 무엇입니까? Heres는
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
을 포함한다.
X
)를 통해 고정 확률 (
p
) "마지막"하위 목록에서 요소를 넣어 함께하고 함께 반복 할 수
내 시도 :
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-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
테스트 :
>>> 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]]
작은 변화 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]]
고정 된 수의 하위 목록이 필요합니까? 그들은 비어있을 수 있습니까? 하위 목록의 최대 길이는 얼마입니까? – RemcoGerlich
죄송합니다. 해당 정보를 원래 게시물에 포함시키지 않았습니다. 충분한 잠은 나의 변명 haha이다. 예, 고정 숫자, 그들은 비어있을 수 없으며 최대 길이는 인수에 설정해야합니다. – Barry
제 대답은 실제로 제 목적에는 잘 작동합니다. 여러분의 제안에 감사드립니다. – Barry