速记索引以获取numpy数组的循环

zour9fqk  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(183)

我想索引numpy数组中的所有元素,并在末尾包含第一个索引。所以如果我有一个数组[2, 4, 6],我想索引这个数组,结果是[2, 4, 6, 2]

import numpy as np
a = np.asarray([2,4,6])

# One solution is 
cycle = np.append(a, a[0])

# Another solution is 
cycle = a[[0, 1, 2, 0]]

# Instead of creating a list, can indexing type be combined?
cycle = a[:+0]
ie3xauqp

ie3xauqp1#

另一种可能的解决方案:

a = np.asarray([2,4,6])
cycle = np.take(a, np.arange(len(a)+1), mode='wrap')

输出:

[2 4 6 2]

相关问题