【发布时间】:2020-05-23 05:56:22
【问题描述】:
我正在尝试将过滤后的值从 Numpy 数组传递到函数中。 我只需要传递某个值以上的值,并将它们的索引位置与 Numpy 数组一起传递。
我试图通过使用 Numpys 自己的过滤系统来避免在 python 中迭代整个数组,我正在处理的数组中有 20k 个值,其中可能只有很少的相关值。
import numpy as np
somearray = np.array([1,2,3,4,5,6])
arrayindex = np.nonzero(somearray > 4)
for i in arrayindex:
somefunction(arrayindex[0], somearray[arrayindex[0]])
这引发了无法处理多个值的逻辑错误, 这导致我通过 print 语句对其进行测试以查看发生了什么。
for cell in arrayindex:
print(f"index {cell}")
print(f"data {somearray[cell]}")
我期望的输出是
index 4
data 5
index 5
data 6
但是我得到了
index [4 5]
data [5 6]
我已经查看了不同的方法来遍历 numpy 数组和 neditor,但似乎没有一个仍然允许我在 for 循环之外对值进行过滤。
我的困惑有解决方案吗?
哦,我知道循环遍历 numpy 数组通常不受欢迎,但是我将这些值传递给的函数很复杂,会触发某些事件并涉及要上传到依赖于数据库的数据数组中的数据位置。
谢谢。
【问题讨论】:
-
这引发了错误 始终共享整个错误消息。我建议阅读以下文章:ericlippert.com/2014/03/05/how-to-debug-small-programs.
标签: python arrays function numpy loops