选择从长度为n的序列米均匀间隔的元素 [英] Choose m evenly spaced elements from a sequence of length n
本文介绍了选择从长度为n的序列米均匀间隔的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有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.
这篇关于选择从长度为n的序列米均匀间隔的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文