【发布时间】:2022-01-01 18:09:00
【问题描述】:
我有一个列表x 的值:
[0.09086322 -0.66400483 -0.85750224, ... 73.92927078, 5.18024081, -17.12200886]
在这里,我想将列表 x 中范围 (-50, 50) 中的值复制到另一个列表 y。
我已经尝试实现以下代码,但它似乎不起作用
y = []
for i in x:
if x[i] >= -50 and x[i] <=50:
y.append(x[i])
我收到以下错误:
only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and
integer or boolean arrays are valid indices
【问题讨论】:
-
你说
list,但是错误提示你使用的是numpy数组。如果是这种情况,并且您想要一个 numpy 数组作为结果,您可以使用y = x[(x >= -50) & (x <= 50)]。这就是为什么发布显示错误的 working 脚本很重要的原因之一。它回答了有关错误是如何真正产生的问题。
标签: python numpy type-conversion