简体   繁体   English

TypeError: 'numpy.bool_' object 不可迭代

[英]TypeError: 'numpy.bool_' object is not iterable

I'm currently encounter a TypeError: 'numpy.bool_' object is not iterable without knowing why.我目前遇到TypeError: 'numpy.bool_' object is not iterable不知道为什么。 I've verified each variables that I used.我已经验证了我使用的每个变量。 Do you have any idea why this error occurs?您知道为什么会发生此错误吗? Here a simplified code of mine that you can use to play with:这是我可以用来玩的简化代码:

import numpy as np


def rad_to_deg(x):
    deg = x*180/np.pi
    return deg

def flag_PA(x,bornes_test):  
    index_0_test = int(np.where(bornes_test==0)[0][0])
    for i in range(index_0_test):
        cond1 = any(bornes_test[i] <= x < bornes_test[i+1])
        cond2 = any(bornes_test[i]+180 <= x < bornes_test[i+1]+180)
        if np.logical_or(cond1,cond2)==True :
            flag_test=i 
    return flag_test


##################################### DATA READING ###########################

# Trouver les fichiers en Bande L et Bande N 


bornes_PA = np.linspace(-180,180,18+1)

lambda_fichier       = np.linspace(3E-6,11E-6)
u_coord_fichier      = np.linspace(1,40)
v_coord_fichier      = np.linspace(1,40)
baseline_fichier     = np.sqrt(np.array(u_coord_fichier)**2+np.array(v_coord_fichier)**2).tolist()
for l in range(len(lambda_fichier)):
    for b in range(len(baseline_fichier)):
        deg = rad_to_deg(np.arctan2(u_coord_fichier[b],v_coord_fichier[b]))
        result = int(flag_PA(deg,bornes_PA))

Here is the full error:这是完整的错误:

  line 34, in <module>
    result = int(flag_PA(deg,bornes_PA))

  line 13, in flag_PA
    cond1 = any(bornes_test[i] <= x < bornes_test[i+1])

The problem is that any tries to iterate over its argument.问题是any试图迭代它的论点。

The expression bornes_test[i] <= x < bornes_test[i+1] returns a scalar numpy.bool_ , not an array.表达式bornes_test[i] <= x < bornes_test[i+1]返回一个标量numpy.bool_ ,而不是一个数组。

Therefore any(bornes_test[i] <= x < bornes_test[i+1]) is trying to iterate over a scalar, as stated by the error message.因此,如错误消息所述, any(bornes_test[i] <= x < bornes_test[i+1])正在尝试迭代标量。

It looks like you can just delete the any and obtain the intended result.看起来您可以删除any并获得预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM