Choose m evenly spaced elements from a sequence of length n

后端 未结 5 1480
再見小時候
再見小時候 2020-12-09 12:24

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.

With m=10, n

5条回答
  •  不知归路
    2020-12-09 13:16

    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 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.

提交回复
热议问题