我有一个索引列表,例如0...365,我想从中选择一些,随机且不重复选择连续的子区间。
index = [i+1 for i in range(365) ]
#n could be 3
for i in range(n):
exclusion_regions.append( get_random_contiguous_region(index) )
有人对于实现get_random_contiguous_region()有什么建议吗?
我有一个索引列表,例如0...365,我想从中选择一些,随机且不重复选择连续的子区间。
index = [i+1 for i in range(365) ]
#n could be 3
for i in range(n):
exclusion_regions.append( get_random_contiguous_region(index) )
有人对于实现get_random_contiguous_region()有什么建议吗?
您可以做以下事情:
import random
n = 3
index = [i+1 for i in range(10) ]
slices = sorted(random.sample(range(0, len(index)), 2*n))
[index[start:end] for start, end in zip(slices[::2], slices[1::2])]
set
,元素是唯一的,所以没有重叠的片段。 - elyaseindexes = range(1, 80)
from random import randint, sample
# recursive division of the sequence
def get_random_division(lst, minsize, maxsize):
split_index = randint(minsize, maxsize)
# if the remaining list would get too small, return the unsplit one
if minsize>len(lst)-split_index:
return [lst]
return [lst[:split_index]] + get_random_division(lst[split_index:], minsize, maxsize)
# determine size range of the subdivisions
minsize, maxsize = 5, int(0.15*len(data))
# choose three of the subdivided sequences
sample(get_random_division(indexes, minsize, maxsize), 3)
输出:
[[17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
[36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46],
[1, 2, 3, 4, 5]]
from random import choice
from numpy import arange
index = [i + 1 for i in range(365)]
choices = []
seen = set()
ar = arange(0.05,.16, .01)
ln = len(index)
sample_size = 0
while sample_size < ln * .30:
perc = choice(ar) # get random 5, 10, 15 percent slices
size = int(ln * perc)
ch = choice(index[:-size+1]) # avoid falling off the side
rn = index[ch:ch+size]
if len(rn) == size and not seen.intersection(rn):
seen.update(rn)
choices.append(rn)
sample_size += len(rn)
print(choices)
index[:-20]
的选择,这样你就永远不会超出范围,如果你想要环绕,也可以,但这需要更多的工作。你想要随机长度的切片还是全部相同长度的切片? - Padraic Cunningham这里提供了一种解决方案,它以符号方式处理范围,而不是考虑每个项目。
(对于您正在处理的小基础,这可能有些过度设计,但对于包含数万项的范围,它将更加高效。)
编辑:我已经更新了代码,现在长度可以被指定为整数或返回整数的0参数函数。现在你可以使用分布来指定长度,而不仅仅是一个常数。
import random
def range_intersection(a, b):
if a.step == b.step == 1:
return range(max(a.start, b.start), min(a.stop, b.stop), 1)
else:
# here be dragons!
raise NotImplemented
def random_subrange(length, range_):
start = random.randrange(
range_.start,
range_.stop - length * range_.step,
range_.step
)
stop = start + length * range_.step
return range(start, stop, range_.step)
def const_fn(n):
def fn():
return n
return fn
def random_distinct_subranges(num, length, range_):
if not callable(length):
length = const_fn(length)
ranges = []
for n in range(num):
while True:
new_range = random_subrange(length(), range_)
if not any(range_intersection(new_range, r) for r in ranges):
ranges.append(new_range)
break
ranges.sort(key = lambda r: r.start)
return ranges
那么
days = range(1, 366)
# pick 3 periods randomly without overlapping
periods = random_distinct_subranges(3, lambda:random.randint(5,15), days)
print(periods)
这将得到类似于这样的结果
[range(78, 92), range(147, 155), range(165, 173)]
from itertools import chain
rand_days = chain(*periods)
print(list(rand_days))
[78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 147, 148, 149, 150, 151, 152, 153, 154, 165, 166, 167, 168, 169, 170, 171, 172]