circular numpy array indices

前端 未结 4 1466
既然无缘
既然无缘 2021-02-20 14:04

I have a 1-D numpy array a = [1,2,3,4,5,6] and a function that gets two inputs, starting_index and ending_index, and returns a[stari

4条回答
  •  自闭症患者
    2021-02-20 14:35

    Unfortunatly you cannot do this with slicing, you'll need to concatonate to two segments:

    import numpy as np
    
    a = [1, 2, 3, 4, 5, 6]
    if starting_index > ending_index:
        part1 = a[start_index:]
        part2 = a[:end_index]
        result = np.concatenate([part1, part2])
    else:
        result = a[start_index:end_index]
    

提交回复
热议问题