gpt4 book ai didi

python - 在 python 中找到特定值矩阵的索引 x,y

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:20 24 4
gpt4 key购买 nike

我将整数列表转换为二维数组,如下所示:

data = numpy.array( l )
shape = ( 10, 30 )
data = data.reshape( shape )

我尝试获取大于某个阈值且低于某个其他阈值的值矩阵的索引 x,y。

我试着做下一个,但它提供了一些错误:

data_indices = numpy.where(data<=obj_value_max and data>=obj_value_min) 

错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

最佳答案

您需要更改您的 where像这样的行:

data_indices = numpy.where((data<=obj_value_max) & (data>=obj_value_min))

注意 ()s围绕每个条件条款和使用 & (意思是“和”)。这是有效的,因为在 numpy 中,<,<=,>,>=,&,|,...被覆盖,即它们的行为与 native python 不同。 andor不能被覆盖,这就是您收到错误消息的原因。

要获取每个值的索引对(而不是(x 索引数组,y 索引数组)),执行

zip(*data_indices)

关于python - 在 python 中找到特定值矩阵的索引 x,y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com