【发布时间】:2021-05-05 14:05:33
【问题描述】:
我对 Numpy 有疑问。基本上我只想在 for 循环中做一个简单的赋值。但奇怪的是,这不起作用。这是我的示例代码:
import numpy as np
test = np.zeros((1280,),dtype=int)
idx = 0
for i in range(32):
test[idx:idx+40] = np.ones((40,),int)*1
idx = idx + 1
print(np.where(test==0))
我会假设循环之后test 中的所有值都等于 1,但是我的程序的输出是
(array([ 71, 72, 73, ..., 1277, 1278, 1279]),)
我不明白为什么数组中仍然有 0 的值。尤其是在索引 72 处,它位于循环的第二次迭代的中间。
你能帮帮我吗? 谢谢!
【问题讨论】:
-
您的意思是将
idx增加40 而不是1? -
这篇文章能回答你的问题吗? post
-
天啊,当然谢谢。我实际上想用 [idx*40:idx*40+40] 进行索引。有点尴尬,我现在已经搜索了2个多小时。再次感谢!
标签: python numpy numpy-indexing