将列表随机分成两个或三个项目的块
Randomly dividing a list into chunks of two or three items
我遇到了将列表拆分为不同大小的块的问题。我想要一个 运行domly 分成 2 对或 3 对的列表。
示例:
L = [1,1,1,1,1,1,1,1,1,1,1,1]
我想得到一些东西:
L2 = [(1,1,1),(1,1),(1,1),(1,1,1),(1,1)]
但我想成为 运行dom,这样每次代码为 运行.
时,成对和三胞胎的分布都会发生变化
将 len(L)
分解为 2 和 3 的块,然后使用循环划分列表。
import numpy as np
def rand23():
return np.random.randint(2,4)
def get_chunk(sz):
rem = sz
ch = []
while ( rem > 0 ):
if ( rem <= 3 ): #if <= 3 add what is left in the chunk (exit condition)
ch.append(rem)
rem = 0
break
elif ( rem == 4 ): #4 is the only edge case here
ch.append(2)
ch.append(2)
rem = 0
break
else:
ch.append(rand23())
rem -= ch[-1]
return ch
L = [1,1,1,1,1,1,1,1,1,1,1,1]
ch = get_chunk(len(L))
L2 = []
count = 0
#Divide the list into chunks based on ch
for c in ch:
L2.append(tuple(L[count:count+c]))
count += c
print L2
结果:(每一行都是一次迭代的输出)
[(1, 1, 1), (1, 1), (1, 1), (1, 1), (1, 1, 1)]
[(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]
[(1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1)]
[(1, 1), (1, 1), (1, 1, 1), (1, 1, 1), (1, 1)]
[(1, 1, 1), (1, 1), (1, 1), (1, 1), (1, 1, 1)]
[(1, 1, 1), (1, 1, 1), (1, 1), (1, 1), (1, 1)]
[(1, 1), (1, 1), (1, 1, 1), (1, 1), (1, 1, 1)]
PS :你也可以递归地实现get_chunk()
。
有很多方法可以解决这个问题,也许你可以编写一个生成器,根据 size
函数生成 returns 大小的块:
import random
def take_chunked(i, size):
idx = 0
while idx < len(i):
s = size(i[idx:])
yield i[idx:idx+s]
idx += s
def size_fun(i):
if len(i) == 4:
return 2
if len(i) <= 3:
return len(i)
return random.randint(2,3)
输出:
>>> list(take_chunked("helloworld", size_fun))
['he', 'll', 'owo', 'rld']
>>> list(take_chunked("helloworld", size_fun))
['hel', 'low', 'or', 'ld']
>>> list(take_chunked("a", size_fun))
['a']
>>> list(take_chunked("", size_fun))
[]
此版本将保证块大小为 2 或 3,只要列表中有足够的项目。
希望对您有所帮助。
第一步:使用随机模块生成随机数
step2:使用随机数来决定它应该是对还是2(对于偶数随机数)/ 3(如果随机数不是偶数)。
第 3 步:编码。
from random import random
def selector():
s = int(random() * 100 )
return (s/2) == 0
L = [1 for i in range(30)]
L2 = []
while L:
if selector():
tmp = 2
else:
tmp = 3
#print tmp
if tmp > 0 and len(L) >= tmp:
L2.append( [ L.pop() for i in range(tmp)] )
if len(L) < 3:
L2.append(set(L))
L = []
print L, L2
作为更通用的方法,您可以使用以下函数:
from itertools import count
import random
def my_split(lst, chunks):
def chunk_creator():
total = 0
while total <= len(lst):
x = random.choice(chunks)
yield L[total: x + total]
total += x
yield total - x
def chunk_finder():
for _ in count():
chunk = list(chunk_creator())
total = chunk.pop(-1)
if total == len(L):
return chunk[:-1]
if max(chunks) <= len(L):
return chunk_finder()
else:
return None
演示:
>>> L = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>> my_split(L, (2, 3))
... [[1, 1], [1, 1], [1, 1], [1, 1, 1], [1, 1, 1]]
>>> my_split(L, (2, 3))
... [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
解释:
此功能由子功能组成。第一个是 chunk_creator 它的工作是根据列表的长度创建所需的块,并将它们 return 作为迭代器。请注意,最终值是 total
变量,它是前面块的总和。
第二个函数 (chunk_finder
) 将通过无限循环 (itertools.count()
) 并检查 total
的值是否等于输入列表的长度。
这个相当简单,可以解决问题:-)
import random
L = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
L2 = list()
i = 0
j = random.randint(2, 3)
while i < len(L):
chunk = L[i:j]
L2.append(chunk)
i = j
if len(L) - i == 4: # case: 4 elements left in L
j = i + 2
elif len(L) - i < 4: # case: 2 or 3 elements left in L
j = len(L)
else:
j = i + random.randint(2, 3)
print L
print L2
我遇到了将列表拆分为不同大小的块的问题。我想要一个 运行domly 分成 2 对或 3 对的列表。
示例:
L = [1,1,1,1,1,1,1,1,1,1,1,1]
我想得到一些东西:
L2 = [(1,1,1),(1,1),(1,1),(1,1,1),(1,1)]
但我想成为 运行dom,这样每次代码为 运行.
时,成对和三胞胎的分布都会发生变化将 len(L)
分解为 2 和 3 的块,然后使用循环划分列表。
import numpy as np
def rand23():
return np.random.randint(2,4)
def get_chunk(sz):
rem = sz
ch = []
while ( rem > 0 ):
if ( rem <= 3 ): #if <= 3 add what is left in the chunk (exit condition)
ch.append(rem)
rem = 0
break
elif ( rem == 4 ): #4 is the only edge case here
ch.append(2)
ch.append(2)
rem = 0
break
else:
ch.append(rand23())
rem -= ch[-1]
return ch
L = [1,1,1,1,1,1,1,1,1,1,1,1]
ch = get_chunk(len(L))
L2 = []
count = 0
#Divide the list into chunks based on ch
for c in ch:
L2.append(tuple(L[count:count+c]))
count += c
print L2
结果:(每一行都是一次迭代的输出)
[(1, 1, 1), (1, 1), (1, 1), (1, 1), (1, 1, 1)]
[(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]
[(1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1)]
[(1, 1), (1, 1), (1, 1, 1), (1, 1, 1), (1, 1)]
[(1, 1, 1), (1, 1), (1, 1), (1, 1), (1, 1, 1)]
[(1, 1, 1), (1, 1, 1), (1, 1), (1, 1), (1, 1)]
[(1, 1), (1, 1), (1, 1, 1), (1, 1), (1, 1, 1)]
PS :你也可以递归地实现get_chunk()
。
有很多方法可以解决这个问题,也许你可以编写一个生成器,根据 size
函数生成 returns 大小的块:
import random
def take_chunked(i, size):
idx = 0
while idx < len(i):
s = size(i[idx:])
yield i[idx:idx+s]
idx += s
def size_fun(i):
if len(i) == 4:
return 2
if len(i) <= 3:
return len(i)
return random.randint(2,3)
输出:
>>> list(take_chunked("helloworld", size_fun))
['he', 'll', 'owo', 'rld']
>>> list(take_chunked("helloworld", size_fun))
['hel', 'low', 'or', 'ld']
>>> list(take_chunked("a", size_fun))
['a']
>>> list(take_chunked("", size_fun))
[]
此版本将保证块大小为 2 或 3,只要列表中有足够的项目。
希望对您有所帮助。
第一步:使用随机模块生成随机数
step2:使用随机数来决定它应该是对还是2(对于偶数随机数)/ 3(如果随机数不是偶数)。
第 3 步:编码。
from random import random
def selector():
s = int(random() * 100 )
return (s/2) == 0
L = [1 for i in range(30)]
L2 = []
while L:
if selector():
tmp = 2
else:
tmp = 3
#print tmp
if tmp > 0 and len(L) >= tmp:
L2.append( [ L.pop() for i in range(tmp)] )
if len(L) < 3:
L2.append(set(L))
L = []
print L, L2
作为更通用的方法,您可以使用以下函数:
from itertools import count
import random
def my_split(lst, chunks):
def chunk_creator():
total = 0
while total <= len(lst):
x = random.choice(chunks)
yield L[total: x + total]
total += x
yield total - x
def chunk_finder():
for _ in count():
chunk = list(chunk_creator())
total = chunk.pop(-1)
if total == len(L):
return chunk[:-1]
if max(chunks) <= len(L):
return chunk_finder()
else:
return None
演示:
>>> L = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>> my_split(L, (2, 3))
... [[1, 1], [1, 1], [1, 1], [1, 1, 1], [1, 1, 1]]
>>> my_split(L, (2, 3))
... [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
解释:
此功能由子功能组成。第一个是 chunk_creator 它的工作是根据列表的长度创建所需的块,并将它们 return 作为迭代器。请注意,最终值是 total
变量,它是前面块的总和。
第二个函数 (chunk_finder
) 将通过无限循环 (itertools.count()
) 并检查 total
的值是否等于输入列表的长度。
这个相当简单,可以解决问题:-)
import random
L = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
L2 = list()
i = 0
j = random.randint(2, 3)
while i < len(L):
chunk = L[i:j]
L2.append(chunk)
i = j
if len(L) - i == 4: # case: 4 elements left in L
j = i + 2
elif len(L) - i < 4: # case: 2 or 3 elements left in L
j = len(L)
else:
j = i + random.randint(2, 3)
print L
print L2