python - 无法确定 NumPy 数组的切片索引

标签 python arrays numpy slice

我有一个 X * Y 的 NumPy 数组元素,表示为扁平数组 ( arr = np.array(x * y) )。
给定以下值:

X = 832
Y = 961
我需要按以下顺序访问数组的元素:
arr[0:832:2]
arr[1:832:2]
arr[832:1664:2]
arr[833:1664:2]
...
arr[((Y-1) * X):(X * Y):2]
从数学上讲,我不确定如何实现 startstop对于循环中的每次迭代。

最佳答案

This should do the trick

Y = 961
X = 832

all_ = np.random.rand(832*961)

# Iterating over the values of y
for i in range(1,Y):
    # getting the indicies from the array we need
    # i - 1 = Start
    # X*i = END
    # 2 is the step
    indicies = list(range(i-1,X*i,2))
    # np.take slice values from the array or get values corresponding to the list of indicies we prepared above
    required_array = np.take(indices=indices)
    

关于python - 无法确定 NumPy 数组的切片索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63635610/

相关文章:

python - 使用零大小内核的高斯模糊?

Python + Mechanize 异步任务

python - 如何用 pandas 和 matplotlib 绘制两个分类(名义)系列之间的比较

php - 在 php 中对数组值进行分组

python - 通过列表理解收集 np 数组的相同行

python - 如何对 Pandas 中的特定层次列求和?

arrays - 如何使用批处理在数组(for 循环)中一次处理两个元素?

javascript - 如何将 JSON 数据数组对象转换为自定义字符串?

python - 在 python 中绘制列表列表(长度不同)

python - 在 Pandas 数据框中进行行相关的正确方法