python随机列表的索引_在Python中随机选择数组中的连续元素

我们需要一个while循环来确保我们不会重叠,你可以检查切片的长度是否符合任何其他条件,使用list comp你不能指定不同的标准:

如果您希望随机切片占总列表大小的大约5%到15%,样本大小大约为30%:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值