选择从长度为n的序列米均匀间隔的元素间隔、序列、均匀、长度为

2023-09-11 01:57:24 作者:懂我不言

我有n个元素的矢量/数组。我想选择m个元素。

I have a vector/array of n elements. I want to choose m elements.

的选择必须是公平/确定性 - 从每款一样多

The choices must be fair / deterministic -- equally many from each subsection.

使用M = 10,N = 20,很容易:只要抓住每一个第二个元素。 但如何做到这一点,在一般的情况下?我一定要计算LCD?

With m=10, n=20 it is easy: just take every second element. But how to do it in the general case? Do I have to calculate the LCD?

推荐答案

下面是一个简单的例子:

Here is a quick example:

from math import ceil

def takespread(sequence, num):
    length = float(len(sequence))
    for i in range(num):
        yield sequence[int(ceil(i * length / num))]

math.ceil 被使用,因为没有它,所选择的指标将被加权太多相向隐含小节的开始,其结果列表作为一个整体

math.ceil is used because without it, the chosen indexes will be weighted too much toward the beginning of each implicit subsection, and as a result the list as a whole.

 
精彩推荐
图片推荐